gpt4 book ai didi

postgresql - 如何检查 Postgres 中的字符串不等式

转载 作者:行者123 更新时间:2023-11-29 13:34:02 25 4
gpt4 key购买 nike

这怎么可能?

select count(*) from happiness where source = 'love';
-[ RECORD 1 ]
count | 0

select count(*) from happiness where source != 'love';
-[ RECORD 1 ]
count | 9279

select count(*) from happiness;
-[ RECORD 1 ]---
count | 1418962...

还有\d

 source                           | character varying(255)      | 

当我得到这个问题的答案时,我会狠狠地打自己的脸。

最佳答案

SQL 使用所谓的“三值”逻辑,给定的 bool 表达式可以是“真”、“假”或“空”。这种形式的查询:

select count(*) from happiness where ...;

将只返回 ... 为“true”的记录,跳过它为“false”或“null”的记录。

source为null时,source = 'love'source != 'love'都为null;因此,您的查询的两个版本都将跳过 source 为空的任何记录。 (NOT (source = 'love') 也将为空:not“true”为“false”,not“false”为“true”,但 not null 仍然是 null。)

这种行为的原因是 null 代表“未知”;如果 source 为 null,则无法知道它是“真的”应该是 'love',还是“真的”应该是其他东西。 (我认为我们倾向于将 null 视为一个独特的值,不同于所有其他值,但这不是它的预期含义。)

你可能想要的是:

select count(*) from happiness where source is null or source != 'love';

关于postgresql - 如何检查 Postgres 中的字符串不等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17385934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com