首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-11 
查询不在另一张表的数据有a表,ID,name,b表,id,groupid,b表的id和a表的id是主外键关系,如何求出在b表中没有

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

热点排行