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

两个表比对问题?
表1:
A                   B               C  
1.0 2.0 2
1.0 3.0 0
1.0 4.0 0
1.0 6.0 2
1.0 16.0 16
...               ...           ...

表2:
a                   b               c
1.0 1.0 2
1.0 1.0 3
1.0 2.0 2
1.0 4.0 0
1.0 6.0 2
...               ...           ...

需要查询表1里有的记录,而表2里没有的记录?

注:3列都相同,才算是表1和表2同时有的




------解决方案--------------------
select * from 表1 where not exists (select 1 from 表2 where a = A and b = B and c = C)
------解决方案--------------------
假设表A和表B都只有两个字段id,name
如何用一句SQL返回表A中存在的id,name结果集而在表B中不存在的id,name结果集

select A.* from A left join B on A.id=B.id and A.name=B.name where B.id is null

select * from A where not exists(select top 1 * from B where A.ID=B.ID)

这两个都可以.


--前提:表中不能有text、ntext、image、cursor 数据类型的字段。

用CheckSum()最简单:

select * from A where checksum(*) not in (select checksum(*) from B)

------解决方案--------------------
--如果查询所有字段

--前提:表中不能有text、ntext、image、cursor 数据类型的字段。

用CheckSum()最简单:

select * from A where checksum(*) not in (select checksum(*) from B)