MySQL LEFT JOIN syntax
August 4, 2005 /
Filed under: SQL
When working with MySQL, sometimes it’s good to check which rows exist in one table, that don’t exist in another. This is only possible when you have two related tables. In other words, a primary key in one table, and a foreign key in the other table. If one table has rows that the other table does not, a match (between the two related tables) will not exist for those "hanging rows." It’s best to remove "hanging rows," to keep your data organized. So, here’s the break-down of what has to be done, in plain English:
Forget checking each row, one by one. MySQL will do the cross-check, and present you with the result, all with a single query:
This is the most basic approach, but extremely effective. There are a few key things to note here, which differ from normal queries.
Comments/Mentions# Mamta Chaurasia at 6/25/2007 12:50 am cst
Could you please tell me, if there is any thing wrong with this query, select u.id,u.fname,u.lname,r.role from users u LEFT JOIN resumes r ON (u.id=r.uid) where u.enabled=1 order by r.flag and r.updated limit 10; where flag is enum value 1,0 and updated is datetime Thanks in advance Mamta # Bosco at 4/16/2009 1:21 am cst
beautiful explanation given for left join.Thanks a lot for having given such a great effort on this |
Recent Comments
Recent Music Listens
|
Excellent tutorial and simplified beautifully.. thanx a lot for this