AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 270265
Accepted
Dimitrios Desyllas
Dimitrios Desyllas
Asked: 2020-07-03 05:46:13 +0800 CST2020-07-03 05:46:13 +0800 CST 2020-07-03 05:46:13 +0800 CST

为什么我的 SELECT 查询不返回空值?[复制]

  • 772
这个问题在这里已经有了答案:
为什么 NOT IN 与包含 NULL 的集合总是返回 FALSE/NULL? (1 个回答)
2年前关闭。

我有以下 SQL 脚本:

CREATE temporary table if not EXISTS the_values (
 key SERIAL,
 value INTEGER NULL 
);

insert into the_values(value) values (null),(1),(null),(2),(3),(4),(5),(6),(10),(null),(null);

select * 
from the_values 
where value not in (1,2,3,4,5,6,10); 

我注意到查询:

select * 
from the_values 
where value not in (1,2,3,4,5,6,10); 

不返回具有valueNULL 的行,这引起了我的注意。因此,我想知道为什么会这样。我对这种现象的技术方面更感兴趣,而不是明显的解决方案:

select * 
from the_values 
where value not in (1,2,3,4,5,6,10) 
   or value IS NULL; 
postgresql null
  • 2 2 个回答
  • 4524 Views

2 个回答

  • Voted
  1. Erwin Brandstetter
    2020-07-03T15:56:53+08:002020-07-03T15:56:53+08:00

    通常,避免任何NOT INNULL一方都可能涉及值。Postgres Wiki也提出了同样的建议。并避免NOT IN (SELECT ...)在任何情况下。维基有解释。并且恰好对您的核心问题有完美的答案:

    1. NOT IN如果存在 null,则会以意想不到的方式表现:
    select * from foo where col not in (1,null); -- always returns 0 rows
    
    select * from foo where col not in (select x from bar);   -- returns 0 rows if any value of bar.x is null
    

    发生这种情况是因为col IN (1,null)返回TRUEif col=1,NULL 否则返回(即它永远不能返回FALSE)。因为NOT (TRUE)is FALSE, but NOT (NULL)is still NULL,所以(这与 col在任何情况下都可以返回 NOT (col IN (1,null))是一样的。NOT IN (1,null))TRUE

    这是一个可能不那么明显的解决方案:

    SELECT * 
    FROM   the_values 
    WHERE  value IN (1,2,3,4,5,6,10) IS NOT TRUE;
    

    或者:

    ..
    WHERE  value = ANY('{1,2,3,4,5,6,10}') IS NOT TRUE;
    

    它比您的“显而易见”的更短且通常更快。

    对于长列表,请考虑切换到不同(更快)的技术:

    • 选择其他表中不存在的行
    • 2
  2. Best Answer
    Lennart - Slava Ukraini
    2020-07-03T05:59:27+08:002020-07-03T05:59:27+08:00

    如果我们将插入和查询简化为:

    insert into T(x) values (null),(1);
    
    select x from T where x not in (1);
    

    对于 null,谓词将评估为:

    select x from T where null not in (1) <=>
    select x from T where not null in (1) <=>
    select x from T where not null <=>
    select x from T where null
    

    因此该行不满足谓词。如果您尝试将某些内容与 null 进行比较(将其视为未知),则结果将为 null。

    对于 1,谓词将评估为:

    select x from T where 1 not in (1) <=>
    select x from T where False
    

    因此该行也不满足谓词

    即你最终得到一个空的结果

    • 1

相关问题

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • 默认为 NULL 还是 NOT NULL?

  • 何时使用 NULL 以及何时使用空字符串?

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve