寻找NOT IN

时间:2019-03-15 17:54:14

标签: postgresql

我认为这可能是PostgreSQL的错误,但是我在这里发布了它,以防万一我丢失了一些东西。当我的WHERE子句具有NOT IN ()子句时,列表中包含null会使该子句不再真实。以下是我问题的简明示例。

=# select 1 where 1 not in (1);
 ?column? 
----------
(0 rows)

=# select 1 where 1 not in (2);
 ?column? 
----------
        1
(1 row)

=# select 1 where 1 not in (null);
 ?column? 
----------
(0 rows)

=# select 1 where 1 not in (null, 2);
 ?column? 
----------
(0 rows)

=# select 1 where 1 not in (2, null);
 ?column? 
----------
(0 rows)

=# select 1 where 1 not in (2, 3);
 ?column? 
----------
        1
(1 row)

由于where 1 not in (1)在列表中,因此1返回了预期的0行,由于where 1 not in (2)不在列表中,而是{{即使1不在列表中,1}}也会返回0行。

1 个答案:

答案 0 :(得分:4)

这不是PostgreSQL的错误。

问题在于,NOT IN只是用于一一测试所有不等式的简短版本。

struct kiocb *iocb; ... iocb = a_work->a_iocb; kfree(a_work); a_work = NULL; /* not really necessary */ iocb->ki_complete(iocb,100, 0); 等效于:

1 NOT IN (null, 2)

但是,1 <> null AND 1 <> 2 是一个特殊值,因此NULL本身就是1 <> null(不是NULL)。参见documentation

  

不要写表达式= NULL,因为NULL不等于NULL。 (空值表示未知值,并且未知两个未知值是否相等。)

据我所知,这是标准的SQL行为。

PostgreSQL还有一个额外的关键字来检查值是否不同于null:

TRUE将是1 IS DISTINCT FROM NULL