752
社区成员
发帖
与我相关
我的任务
分享
如何从表A(总表)中找出表B(分表)中不存在的记录组合
假设表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)
----------------------------------------------------------------------
查询表A,表B中重复的记录
select * from A where checksum(*) in (select checksum(*) from B)
查询表A,表B中重复的记录,并把结果输入到表C中。
select * into c from A where checksum(*) in (select checksum(*) from B)