Mysql: Select rows from a table that are not in another Mysql: Select rows from a table that are not in another mysql mysql

Mysql: Select rows from a table that are not in another


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:

SELECT * FROM Table1 WHERE id NOT IN (SELECT id FROM Table2)

Refer to the MySQL subquery syntax for more examples.


If you have 300 columns as you mentioned in another comment, and you want to compare on all columns (assuming the columns are all the same name), you can use a NATURAL LEFT JOIN to implicitly join on all matching column names between the two tables so that you don't have to tediously type out all join conditions manually:

SELECT            a.*FROM              tbl_1 aNATURAL LEFT JOIN tbl_2 bWHERE             b.FirstName IS NULL


SELECT *FROM Table1 AS aWHERE NOT EXISTS (  SELECT *  FROM Table2 AS b   WHERE a.FirstName=b.FirstName AND a.LastName=b.Last_Name)

EXISTS will help you...