两个表比对问题?

✪枫✪ 2007-09-26 12:22:06
表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同时有的


...全文
107 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2007-09-26
  • 打赏
  • 举报
回复
--如果查询所有字段

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

用CheckSum()最简单:

select * from A where checksum(*) not in (select checksum(*) from B)
dawugui 2007-09-26
  • 打赏
  • 举报
回复
假设表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)
yrwx001 2007-09-26
  • 打赏
  • 举报
回复
select * from 表1 where not exists (select 1 from 表2 where a = A and b = B and c = C)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧