求一个集合的差集

maxqinx 2010-12-03 10:56:12
SQL server里只有 交集合集并集 但我没看过差集 怎么求差集啊?
...全文
96 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
maxqinx 2010-12-03
  • 打赏
  • 举报
回复
select a.* from a where id not in (select id from b)
用这个就可以了 后面的 union all 不用了
dawugui 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 maxqinx 的回复:]
不懂。我就是想把A表和B表的重复ID去掉 求出剩下ID的 Name啊 什么的别的字段信息
[/Quote]
select a.* from a where id not in (select id from b)

select b.* from b where id not in (select id from a)

select a.* from a where id not in (select id from b)
union all
select b.* from b where id not in (select id from a)
maxqinx 2010-12-03
  • 打赏
  • 举报
回复
不懂。我就是想把A表和B表的重复ID去掉 求出剩下ID的 Name啊 什么的别的字段信息
dawugui 2010-12-03
  • 打赏
  • 举报
回复
--sql 2000
如何从表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)


--sql 2005
except
jwdream2008 2010-12-03
  • 打赏
  • 举报
回复
Except
飘零一叶 2010-12-03
  • 打赏
  • 举报
回复
except
dawugui 2010-12-03
  • 打赏
  • 举报
回复
not in
not exists

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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