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

使用not in和 not exists这两sql为啥结果不一样?该怎么处理

2012-01-10 
使用not in和 not exists这两sql为啥结果不一样?selectb.*fromtmp_ddna,tmp_ddnbwhereb.order_item_ida.r

使用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 ))

热点排行