当前位置: 代码迷 >> Sql Server >> 查询不在另一张表的数据,该怎么处理
  详细解决方案

查询不在另一张表的数据,该怎么处理

热度:29   发布时间:2016-04-27 20:51:35.0
查询不在另一张表的数据
有a表,ID,name,b表,id,groupid,b表的id和a表的id是主外键关系,如何求出在b表中没有a表的数据。

------解决方案--------------------
select * from b
where not exists(select 1 from a where a.id=b.id)
------解决方案--------------------
declare @a table(id int)
declare @b table(id int)

insert @a select 1 union all select 2
insert @b select 1 union all select 2 union all select 3


select *
from @b b
where not exists(select id from @a where id = b.id)


select *
from @b b
where id not in (select id from @a )
  相关解决方案