IN , NOT IN 用于 oracle 中的空值

Posted

技术标签:

【中文标题】IN , NOT IN 用于 oracle 中的空值【英文标题】:IN , NOT IN for null value in oracle 【发布时间】:2018-06-22 05:57:53 【问题描述】:

在 oracle 中,为什么“Not in”对空值不起作用,但“IN”起作用 例如

  with temp(n,p) as (
  select 1,2 from dual union all
  select 3,2 from dual union all
  select 4,6 from dual union all
  select 5,6 from dual union all
  select 2,8 from dual union all
  select 6,8 from dual union all
  select 8,null from dual
  )
1. Select * from temp where n in (2,6,8,null);
2. Select * from temp where n not in (2,6,8,null);

First Statement 将给出输出 = 2,6,8 第二条语句不会给出任何输出

谁能解释一下原因?

【问题讨论】:

它可以正常工作。你期待什么结果? 重复SQL not displaying null values on a not equals query? 【参考方案1】:

NOT IN 本质上是这样工作的:

col NOT IN (value_a, value_b, value_c)
-- is the same as
col != value_a && col != value_b && col != value_c

如果其中一个值是null,则整个表达式的计算结果为null,而不是true(您可能期望如此)。

您可以在此处阅读更多信息:https://jonathanlewis.wordpress.com/2007/02/25/not-in/

【讨论】:

以上是关于IN , NOT IN 用于 oracle 中的空值的主要内容,如果未能解决你的问题,请参考以下文章