in he not in 有什么区别吗?
我有两张一模一样的表表A 表B
表A的CODE唯一,表B的code唯一
表A有900条数据,表B有600条数据我select * from a where code in(select code from b) 结果有600条
可是当我select * from a where code not in(select code from b)的时候,结果都是0
那我那300条数据丢哪?

解决方案 »

  1.   

    你这个是查询A里面的CODE在B表里面存在的数量 很正常
      

  2.   

    可是当我select * from a where code not in(select code from b)的时候,结果都是0这个是不是A表里有的B表没有的都该出来?
      

  3.   

    因为你B里面只有600条数据 所以查询在B里面的CODE只有600条很正常而你所谓的那300条一定不存在于B中 所以你第二句结果一定是0
      

  4.   

    select * from a, b where a.code = b.code
    先检查一下a,bcode相同的数据数目了
      

  5.   

    select * from b where code not in(select code from a)换过来试试就知道怎么回事了
      

  6.   

    不应该呀declare  @tablea table (id int)
    insert into @tablea
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5 union all
    select 6 union all
    select 7 union all
    select 8 union all
    select 9declare @tableb table(id int)insert into @tableb
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5 union all
    select 6 
    select * from @tablea where id in(select id from @tableb) select * from @tablea where id not in(select id from @tableb)
    /*
    id
    -----------
    1
    2
    3
    4
    5
    6
    id
    -----------
    7
    8
    9*/
      

  7.   

    我后面是not in啊 不在B中的,应该返回300条才对啊?难道,天资聪颖的我理解错了?
      

  8.   

    你确定A表中code字段唯一?
    --建议你执行下面语句看下这个A表中code是否唯一
    select code from A group by code having count(1)>1
      

  9.   

    --你这样试下 有点奇怪哦
    select * from a t where  not exists(select 1 from b where t.code=b.code)
      

  10.   

    有一个条件我说错了,不好意思,A表900条数据,B表有2000条数据,但 A表和B表code相同的只有600条,现在反过来select * from b where cdoe not in(select code from a)返回结果有1400条,而
    select * from a where code not in (select code from b)返回的结果是0条,理论上应该是300对不?
      

  11.   


    我group 过了,的确是唯一的
      

  12.   

    code 唯一的情况下应该是300条
      

  13.   


    出来了,是300条,为啥NOT IN 就不行呢?
      

  14.   


    表B里面的确有部分数据是NULL的,刚才topest0302老大说group 我只做了表A的,
      

  15.   

    NULL值的神奇之处:
    DECLARE @I INT
    SELECT 1 WHERE @I=0 OR @I<>0
    --无返回记录
      

  16.   

    求解释,为什么是null就会返回0
      

  17.   

    DING
    正常数据了,b里的a都有
      

  18.   


    NULL值引起的,请用下面的查询试试select * from a where code in(select isnull(code,'') from b)
    select * from a where code not in(select isnull(code,'') from b)
      

  19.   

    b.code 只要有一条是null,你not in的结果就为0