Jump to content

Left join confusion


rockinaway

Recommended Posts

I have a normal query for the database and I was to do a left join. However, the data in this left join needs to be ordered first and only the latest values take (i.e. only one value taken).

 

I'm confused if I can include an order by for the left join and a where clause with it as well.

 

Would it be better to just have another query?

Link to comment
https://forums.phpfreaks.com/topic/247274-left-join-confusion/
Share on other sites

So far:

 

SELECT m.mind, id, first_name, last_name 
					        FROM users
					        LEFT JOIN minds AS m ON m.u_id = id
					        ORDER BY m.time
					        WHERE email = "'.$email_sent.'"

 

Do I need to put users as u and then use that as well?.. 

Link to comment
https://forums.phpfreaks.com/topic/247274-left-join-confusion/#findComment-1269973
Share on other sites

Okay, I've worked on it, and it's having the desired effect currently, but I want to check the query is doing what I hope it is:

 

$query = 'SELECT t.mind, m.id, m.first_name, m.last_name
											FROM users AS m
											LEFT JOIN minds AS t ON t.u_id = m.id
					       					WHERE m.email = "'.$email_sent.'"
					       					ORDER BY t.time DESC LIMIT 1';

 

So I want to collect data from the users table for that one user. And then I want to get the information from the minds table. In the minds table, each user has several rows and so I want to order them in descending order by their time and then take the first one and add it to the rest of the user data..

 

 

Link to comment
https://forums.phpfreaks.com/topic/247274-left-join-confusion/#findComment-1269984
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.