Compare two identical tables MySQL

*爱你&永不变心* 提交于 2019-12-11 07:38:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!