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.
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
- The holes with doughnuts
- Send SMS with a browser? Isn’t that email?
- Social networking: say it; don't spray it
Similar Entries
- Install Apache, PHP, MySQL on Windows (221 recent visits)
- MySQL search criteria - column alias (1371 recent visits)
- MySQL integer columns and display width (1346 recent visits)
- MySQL viewing saved searches (30 recent visits)
- MySQL changing column types (908 recent visits)
- MySQL: enum or char(1)? (558 recent visits)
Stats
3522 unique visits since August 2008
Recent Referrers (click)
- Mysql left join
- left join null
- left join in mysql
- left join in mysql
- mysql left join syntax
- mysql left joins
- left join in mysql
- mysql left join
- mysql left join
- LEFT JOIN SYNTAX
- left join, mysql
- mysql left join
- mysql join on
- mysql left join null values
- left join syntax
- mysql joining null values
- mysql left join
- left join mysql
- mysql left join
Excellent tutorial and simplified beautifully.. thanx a lot for this ... Read more.