当前位置: 代码迷 >> Sql Server >> 求2张表的差集,这个sql如何写
  详细解决方案

求2张表的差集,这个sql如何写

热度:26   发布时间:2016-04-24 18:44:00.0
求2张表的差集,这个sql怎么写?
b表是a表的子集,现在要查询出(a-b),这个sql怎么写
------解决方案--------------------
试试这个:
select *
from a
where not exists(select 1 from b where a.id = b.id)

------解决方案--------------------
上面的主要是通过a和b表的关联字段来判断的。

如果是多个字段,那么可以改为:

select *
from a
where not exists(select 1 from b 
                 where a.col1 = b.col1 and
                       a.col2 = b.col2 and
                       a.col3 = b.col3)
  相关解决方案