Compare two tables/fields

By | 30 March, 2009

Found this somewhere on google.

If we need to compare two tables, this query will list the missing records on one of them (on table2):

SELECT t1.id
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL

Then you do it the other way around to check the mising records on the other table (table1).

To check for different values in identical fields:

SELECT t1.id
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id
WHERE t1.field1 <> t2.field1
OR t1.field2 <> t2.field2

etc…