有两个表:表a 和表b,有共同的一列:bh,表a的bh列中有表b中bh列中没有的数据,现在我要挑出表a的列bh中有而表b中bh的列没有的的所有的数据所在行行。语言如下:
select *
from a
where bh not in (select bh from b);
但结果连一行数据都没有,为什么啊

解决方案 »

  1.   

    你这sql没错  是b表包含a表吧?反过来看看最好不要用in 
    select select * from a where not exists(select 1 from b where a.bh=b.bh)
      

  2.   

    谢谢,我第一次来这个网站想不到这么快回复。是a表包含b表,a表有1450行,b表有1175行,所以a表>表b。我明天会试试你的推荐语句行不行,谢谢,还有:select 1 from b where a.bh=b.bh)中的1是什么意思啊?
      

  3.   

    行多不一定就是a包含b
    如:
    a表
    --
    a
    d
    cb 表
    ------------
    a
    a
    c
    c
    c1在这没意思,你明白exists 就可以了~只返回一个真假,1你想换什么都可以,1可以说是没意义的
      

  4.   

    你好,谢谢你的回复。我刚起床,怎么加你为好友啊,其实我b表中的数据确实是a表中数据的一部分。a表有一些b表中没有的数据,就拿你的这个例子来说,应该返回d这一列对不对?我现在工作急用这个语句,我正在核对一些数据。你的语句我要到办公室试试才行,这儿没数据。刚才跟你聊天发不了信息,就又回复拉
      

  5.   

    --根据你说的没有错误啊
    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)
    你再检查一下其它的原因吧
      

  6.   

    select *
    from a
    where bh not in (select bh from b);
    ----------
    沒有分號,這句話是沒有錯的,只b中有a 的記錄是不會顯示出來的orselect * from a where not exists(select 1 from b where a.bh=b.bh)
    要快一點
      

  7.   

    友情提醒:
    注意...
    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)
    意思不一样!!!
      

  8.   

    我刚刚试过啦not exists语句是正确的,但用not in 不行,虽然我搞不清楚怎么回事,但总算解决了一个问题.谢谢大家