使用not in和 not exists这两sql为啥结果不一样?

csu7433rj 2007-06-28 12:52:52
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 )

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

...全文
416 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzm1117 2008-12-01
  • 打赏
  • 举报
回复
good 不错
csu7433rj 2007-06-29
  • 打赏
  • 举报
回复
搞清楚了

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 )
改成
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 where sn_97 is not null )
and a.sn_97 not in ( select sn_97 from tmp_ddn_shangwang where sn_97 is not null )
结果就一样了
csu7433rj 2007-06-29
  • 打赏
  • 举报
回复
应该不是空值的问题,我试了第一句sql返回600多条记录,第二条返回的记录为空。高手再帮我看看阿,我就想弄到底问题出在哪
多壮志 2007-06-28
  • 打赏
  • 举报
回复
如果不想选择出非空的部分,在第一语句中加 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 ))
csu7433rj 2007-06-28
  • 打赏
  • 举报
回复
“有空值是会不一样。”

那要怎样修改才能让他们结果一样呢???
csu7433rj 2007-06-28
  • 打赏
  • 举报
回复
能否具体说说呀?
wwwzjb 2007-06-28
  • 打赏
  • 举报
回复
not exists比not in 的性能要高,也是官方推荐的用法
kingofworl 2007-06-28
  • 打赏
  • 举报
回复
sn_97 有空值
bobfang 2007-06-28
  • 打赏
  • 举报
回复
有空值是会不一样。

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧