日期:2014-05-17  浏览次数:20945 次

使用not in和 not exists这两sql为啥结果不一样?
select   b.*   from   tmp_ddn   a,tmp_ddn   b
where   b.order_item_id   =   a.root_order_item_id   and
    not   exists(   select   *   from   tmp_ddn_guoji   where   sn_97   =   a.sn_97   )
and   not   exists(   select   *   from   tmp_ddn_vpn   where   sn_97   =   a.sn_97   )
and   not   exists(   select   *   from   tmp_ddn_shangwang   where   sn_97   =   a.sn_97   ))


select   b.*   from   tmp_ddn   a,tmp_ddn   b
where   b.order_item_id   =   a.root_order_item_id   and
  a.sn_97   not   in   (   select   sn_97     from   tmp_ddn_guoji   )
              and   a.sn_97   not   in   (   select   sn_97     from   tmp_ddn_vpn   )
              and   a.sn_97   not   in   (   select   sn_97     from   tmp_ddn_shangwang   )

这两语句的意思应该相同吧?为什么结果不同?



------解决方案--------------------
有空值是会不一样。
------解决方案--------------------
sn_97 有空值
------解决方案--------------------
如果不想选择出非空的部分,在第一语句中加 A.SN_97 IS NOT NULL 即可。
select b.* from tmp_ddn a,tmp_ddn b
where b.order_item_id = a.root_order_item_id and (A.SN_97 IS NOT NULL)
not exists( select * from tmp_ddn_guoji where sn_97 = a.sn_97 )
and not exists( select * from tmp_ddn_vpn where sn_97 = a.sn_97 )
and not exists( select * from tmp_ddn_shangwang where sn_97 = a.sn_97 ))