读书人

两个表比对有关问题

发布时间: 2012-02-16 21:30:36 作者: rapoo

两个表比对问题?
表1:
A B C
1.02.02
1.03.00
1.04.00
1.06.02
1.016.016
... ... ...

表2:
a b c
1.01.02
1.01.03
1.02.02
1.04.00
1.06.02
... ... ...

需要查询表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)

读书人网 >SQL Server

热点推荐