postgresql 如何选择列值为空的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34370788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:06:54  来源:igfitidea点击:

How to select rows having column value as null?

sqlsql-serverpostgresql

提问by afzalex

Consider following database table:

考虑以下数据库表:

id      |  is_visible
________|_______________
1       |  true
2       |  false
3       |               -- i.e. null

I want to select all the rows having is_visible = null. I tried the following queries:

我想选择所有具有 is_visible = null 的行。我尝试了以下查询:

SELECT * FROM tab WHERE is_visible = null;     -- 0 results
SELECT * FROM tab WHERE is_visible <> true;    -- only fields with false value
SELECT * FROM tab WHERE is_visible <> false;   -- only fields with true

I tried it in postgresql and sql server
How to select rows having is_visible = null?

我在 postgresql 和 sql server 中尝试过
如何选择具有 is_visible = null 的行?

回答by chaudharyp

In both Postgres and SQL server,

在 Postgres 和 SQL 服务器中,

SELECT * FROM tab WHERE is_visible is null;

If you want to select the rows for which column values are not null, then use is not nulloperator:

如果要选择列值不为空的行,请使用is not null运算符:

SELECT * FROM tab WHERE is_visible is not null;

回答by Sundar Singh

you can't compare 2 null values in sql server as null is unknown or not available. https://technet.microsoft.com/en-us/library/aa196339(v=sql.80).aspx

您无法比较 sql server 中的 2 个 null 值,因为 null 未知或不可用。 https://technet.microsoft.com/en-us/library/aa196339(v=sql.80).aspx

Use IS NULL operator:

使用 IS NULL 运算符:

SELECT * FROM tab WHERE is_visible IS NULL;  -- only field having null value
SELECT * FROM tab WHERE is_visible IS NOT NULL;    -- fields having not null values