读书人

用一个SQL查询语句查询两个表的数据是

发布时间: 2012-03-11 18:15:39 作者: rapoo

用一个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 )

读书人网 >SQL Server

热点推荐