问题
I'm currently in the process of converting data, in a table, which is why I've created a new table, identical to the old one, but empty.
I've run my data converter, and I have a difference in row count.
How do I select all rows that are different from the two tables, leaving out the primary key identifier (that differs on every entry).
回答1:
select * from (
SELECT 'Table1',t1.* FROM table1 t1 WHERE
(t1.id)
NOT IN (SELECT t2.id FROM table2 t2)
UNION ALL
SELECT 'Table2',t2.* FROM table2 t2 WHERE
(t2.id)
NOT IN (SELECT t1.id FROM table1 t1))temp order by id;
You can add more columns in where columns to check on more info. Try and see if this helps.
回答2:
This gives you all rows that are present in t1 and not in t2. You list all columns except for the ID. (And of course you can switch this to get all rows present in t2 and not in t1 :-)
select col1, col2, col3, ... from t1
except
select col1, col2, col3, ... from t2;
来源:https://stackoverflow.com/questions/40171288/compare-two-identical-tables-mysql