繁体   English   中英

NOT EXIST 和 NOT IN 有什么区别?

[英]what is the difference between NOT EXIST and NOT IN?

特别是在处理 NULL 值时我将如何选择使用哪个?

您应该始终对子查询使用not exists 它以直观的方式处理NULL值。

如果你有:

where t.id not in (select x.id from x)

并且x.id永远NULL ,那么根本不返回任何行 这是因为NULL值的定义。

等效公式:

where not exists (select 1 from x where x.id = t.id)

行为如您所愿。

not exists另一个优点是您可以一次处理多个列:

where not exists (select 1 from x where x.id = t.id and x.date = t.date)

某些数据库支持元组,这允许您将其表示为not in ,但并非所有数据库都支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM