MySQL LEFT JOIN syntax Aug04 '05
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:
I have to go into each table, and cross check each row, against one another. So, for each row that exists in the first table, I have to make sure there is a match in the second table. This could be incredibly time consuming, as there are hundreds of rows in both tables.
Forget checking each row, one by one. MySQL will do the cross-check, and present you with the result, all with a single query:
SELECT table_1.row_1, table_2.row_1
FROM table_2
LEFT JOIN table1 ON table_1.row_1 = table_2.row_1
WHERE table_1.row_1 IS NULL
This is the most basic approach, but extremely effective. There are a few key things to note here, which differ from normal queries.
- There is only one table specified in the
FROMclause, even though we are checking two tables. The table that should be specified in theFROMclause is the one that you want to "clean up." In other words - we’d like to remove the "hanging rows" fromtable_2. - Notice that, although we only specify
table_2in theFROMclause, we still specify a row fromtable_1, in theSELECTclause. This may seem odd to some, but the next step will explain. - The
LEFT JOINsyntax involves the other table that we are cross-checking against - in this case,table_1. - The
ONclause allows us to specify the primary key relation to the foreign key, across the two tables. In other words, this is where the "cross check" happens. MySQL compares all of the primary keys against all of the foreign keys (which is what we would be doing manually). - When MySQL finds foreign keys that don’t match primary keys, it returns a
NULLvalue. - The
WHEREclause specifies that we only want to see thoseNULLvalues. Because... thoseNULLvalues are the rows that exist in one table, but not the other. That’s the information we’re after.
Categories: SQL ![]()
Add Feedback (view all)
Leave feedback
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 ... Read more.
Thank you! It was just what I was looking for. ... Read more.
- The holes with doughnuts
- Send SMS with a browser? Isn’t that email?
- Customize Brightkite-to-Twitter updates
Popular Pages
- Fast rounded corners in Photoshop (7424 recent visits)
- PHP – passing variables across pages (2558 recent visits)
- JavaScript set selected on load (2413 recent visits)
- Removing all child nodes from an element (1828 recent visits)
- Firefox 3 smart address bar: wildcard search (1755 recent visits)
- iPod songs out of order? (1314 recent visits)
- Britney - Everytime piano tab (1137 recent visits)
- MySQL LEFT JOIN syntax (884 recent visits)
- Firefox 3 smart address bar: wildcard search (722 recent visits)
- Date difference in MySQL (651 recent visits)
Similar Entries
- Install Apache, PHP, MySQL on Windows (25 recent visits)
- MySQL search criteria - column alias (225 recent visits)
- MySQL integer columns and display width (250 recent visits)
- MySQL viewing saved searches (12 recent visits)
- MySQL changing column types (119 recent visits)
- MySQL: enum or char(1)? (121 recent visits)
Stats
884 unique visits since July 2008
Recent Referrers (click)
- LEFT JOIN mysql
- left join mysql where
- mysql join check for null
- left join
- left join mysql
- http://matthom.com/
- mysql select from row from other table
- mysql join syntax tables
- mysql syntax
- join in mysql
- mysql row in one table but not in other table
- mysql join
- mysql select row from one table and does not exists in other table
- mysql join not exist
- left join mysql
- left join on clause syntax
- mysql left join
Excellent tutorial and simplified beautifully.. thanx a lot for this ... Read more.