How to Select Rows in table A that are not in table B
November 30th, 2008
Here is another trick from Jerry,
SELECT a.id, a.warna FROM table_a a LEFT JOIN table_b b ON a.id = b.id WHERE b.id IS NULL OR a.warna<>b.warna;
is used for selecting data from table a which is not same like table b. Thanks Jerry.
If you want to select value from table a that are not in table b, then you can use this query in mysql:
How do I select all rows in table `a` that is not in table `b`:
SELECT a.id FROM a LEFT JOIN b ON a.id = b.aid WHERE b.aid IS NULL;
a.id can be changed with value you use, example:
SELECT a.warna FROM a LEFT JOIN b ON a.warna = b.warna WHERE b.warna IS NULL;
Because “SELECT a.id” simply decides which fields will be output this will work just as well:
SELECT * FROM a LEFT JOIN b ON a.id = b.aid WHERE b.aid IS NULL;
Or
SELECT * FROM a LEFT JOIN b ON a.warna = b.warna WHERE b.warna IS NULL;
Another interesting posts:
- How adding another table to JOIN can improve performance ?
- JOIN Performance & Charsets
- How expensive is a WHERE clause in MySQL?


Recent Comments