当前位置: 代码迷 >> Sql Server >> 有关 not in 的有关问题
  详细解决方案

有关 not in 的有关问题

热度:733   发布时间:2016-04-27 20:08:16.0
有关 not in 的问题
有两个表:表a   和表b,有共同的一列:bh,表a的bh列中有表b中bh列中没有的数据,现在我要挑出表a的列bh中有而表b中bh的列没有的的所有的数据所在行行。语言如下:
select   *
from   a
where   bh   not   in   (select   bh   from   b);
但结果连一行数据都没有,为什么啊

------解决方案--------------------
你这sql没错 是b表包含a表吧?

反过来看看

最好不要用in
select select * from a where not exists(select 1 from b where a.bh=b.bh)
------解决方案--------------------
--根据你说的没有错误啊
declare @t table(id int,name varchar(10))
insert @t
select 1, 'aa '
union all select 3, 'cc '
union all select 4, 'c '
union all select 5, 'dd '
union all select 6, 'e '
declare @b table(id int,name varchar(10))
insert @b
select 1, 'a '
union all select 3, 'c '
union all select 4, 'ce '
union all select 7, 'ddd '

select * from @t
where id not in (select id from @b)
你再检查一下其它的原因吧
------解决方案--------------------
select *
from a
where bh not in (select bh from b);
----------
沒有分號,這句話是沒有錯的,只b中有a 的記錄是不會顯示出來的

or

select * from a where not exists(select 1 from b where a.bh=b.bh)
要快一點
------解决方案--------------------
友情提醒:
注意...
select * from a where not exists(select 1 from b where a.bh=b.bh)

select * from a where not in(select 1 from b where a.bh=b.bh)
意思不一样!!!
------解决方案--------------------
语句是没有错.
  相关解决方案