How to select all rows in one table that do not appear on another?
Table1:
+-----------+----------+------------+
| FirstName | LastName | BirthDate |
+-----------+----------+------------+
| Tia | Carrera | 1975-09-18 |
| Nikki | Taylor | 1972-03-04 |
| Yamila | Diaz | 1972-03-04 |
+-----------+----------+------------+
Table2:
+-----------+----------+------------+
| FirstName | LastName | BirthDate |
+-----------+----------+------------+
| Tia | Carrera | 1975-09-18 |
| Nikki | Taylor | 1972-03-04 |
+-----------+----------+------------+
Example output for rows in Table1 that are not in Table2:
+-----------+----------+------------+
| FirstName | LastName | BirthDate |
+-----------+----------+------------+
| Yamila | Diaz | 1972-03-04 |
+-----------+----------+------------+
Maybe something like this should work:
SELECT * FROM Table1 WHERE * NOT IN (SELECT * FROM Table2)
You need to do the subselect based on a column name, not
*
.For example, if you had an
id
field common to both tables, you could do:Refer to the MySQL subquery syntax for more examples.
EXISTS
will help you...A standard LEFT JOIN could resolve the problem and, if the fields on join are indexed,
should also be faster
Try:
Try this simple query. It works perfectly.