日期:2014-05-18  浏览次数:20397 次

用一个SQL查询语句查询两个表的数据是否一致,不一致的数据显示出来
我想用一个SQL查询语句查询两个表的数据是否一致,不一致的数据显示出来
(注:这两个表的数据是一样的,不过后来有一个表删除了一些数据,现在想把另一个表没有删除的数据查找出来)
例如:A表和B表,那应该如何写这个sql语句.以name字段为查询字段.
请指教.

------解决方案--------------------
--假如B刪了一些
select A.* from A
where not exists(select 1 from B where A.name=B.name)
------解决方案--------------------
select * from a a where not exists(select * from b b where a.name=b.name)
------解决方案--------------------
name字段在表中是否是主键

如果是的话可以这样写
select * from B where name not in (select name from A)

ps: A表为删除过数据的表
------解决方案--------------------
select A.* from A left join B
ON A.KEY=B.KEY
WHERE B.KEY IS NULL
------解决方案--------------------
A为完整的表,KEY为关键字段
------解决方案--------------------
--假如B刪了一些
select * from A where name not in (select NAME from B )