gpt4 book ai didi

sql - 当 != 时,Postgres 不返回值为 NULL 的行

转载 作者:行者123 更新时间:2023-11-29 11:52:54 26 4
gpt4 key购买 nike

我在这里看到一个线程:Why does PostgreSQL not return null values when the condition is <> true有解释为什么会发生这种情况的答案,但我仍然不确定如何解决它。

我正在运行与此类似的查询:

SELECT * FROM beers WHERE name != 'Budlight';

我希望它返回 name 不等于 Budlight 的行。结果应包括名称为 NULL 的行。相反,我的结果显示 name 不是 BudlightNULL 的行。

我怎样才能形成只从结果中省略名称不是 Budlight 的行的查询?

SQLFiddle:http://www.sqlfiddle.com/#!15/7b9bd/1

最佳答案

Postgres 提供了 is distinct from 运算符。你可以这样做:

SELECT *
FROM beers
WHERE name IS DISTINCT FROM 'Budlight';

这可以满足您对 NULL 值的期望。

这个运算符在 documentation 中有解释。 .

关于sql - 当 != 时,Postgres 不返回值为 NULL 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38779664/

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