Postgresql postgresnotin(null)不给出结果

Postgresql postgresnotin(null)不给出结果,postgresql,null,notin,Postgresql,Null,Notin,我正在使用Postgres进行此查询 select * from Entity this_ where (this_.ID not in (null)) 为什么这没有给我结果?我希望得到id不为null的所有行 与 我得到了预期的结果在(null)中的[not]结果将始终为null。要与null进行比较,您需要是[not]null或是[not]与null不同的 select * from Entity this_ where this_.ID is not null 如果您希望whe

我正在使用Postgres进行此查询

select 
*
from Entity this_ 
where 
(this_.ID not in (null))
为什么这没有给我结果?我希望得到id不为null的所有行


我得到了预期的结果

在(null)中的
[not]结果将始终为null。要与null进行比较,您需要
是[not]null
是[not]与null不同的

select *
from Entity this_ 
where this_.ID is not null
如果您希望
where(ID不在(1,null)中))
与您的注释一样,您可以这样做

where ID is not null and ID not in (1)

PostgreSQL使用NULL作为未定义的值。
您要求的是返回不在列表中的项或未定义的值。由于未定义意味着您不知道其中的内容,PostgreSQL不会返回任何项目,因为无法响应请求。
虽然请求:

select * from Entity where id in (1, null)
select * from Entity where (ID not in (1, null))
可以返回记录,因为如果它找到ID=1的元素,则知道该元素在集合中
请求:

select * from Entity where id in (1, null)
select * from Entity where (ID not in (1, null))
无法满足,因为空值可以是任何值

select * 
from Entity this_ 
where (this_.ID not in (null))
“IN”或“NOT IN”不选择空值 你可以写

select * 
from Entity this_ 
where (this_.ID not in (1))
并且您的选择将不包含空值

您可以使用任何运算符。 代码示例:

select 
  * 
from Entity this_ 
where 
   (this_.ID <> ANY (null))
选择
* 
从这个实体
哪里
(此ID不存在(空))

我在问题中遇到了类似的,并最终提出了以下解决方案

select * from public."Employee_11" where (COALESCE("Name",'@'),"Surname") 
    in (
        ('@','dummy')
    )
它确实返回Name列具有空值的记录。您还可以将其用于not in子句,该子句将返回Name的非空记录

  select * from public."Employee_11" where (COALESCE("Name",'@'),"Surname") 
        not in (
            ('@','dummy')
        )

我也有类似的问题。我对SQL很了解的自尊心被刺穿了。 下面是scott/tiger表格中的简化示例

select empno, ename from emp where deptno not in (10, 20, null);
什么也没回来。虽然我使用不在条件非常节省,因为它不使用索引,而且非常缓慢。我更喜欢使用外部联接


我在Postgres和Oracle中尝试了这个查询,结果都是一样的。因此,必须是符合标准的结果。空值仅在不在条件下才会以这种方式运行。

表示不可能进行类似“从实体中选择*(ID不在(1,空))”的选择。欢迎!这个答案似乎重复了本页其他地方答案中已经包含的信息,而不是对问题的新答案。如果您还没有,您可能想看看,它解释了一点这个网站的工作原理。它的行为就像NOT IN一样,null不返回任何内容。