日期:2014-05-19  浏览次数:20528 次

查询不在另一张表的数据
有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 )