SQL 查询以从列中获取空计数

SQL query to get null count from a column

我想生成一个 SQL 查询以获取特定列中的空计数,例如

SELECT COUNT (column) AS count 
FROM table 
WHERE column = null ;

这将返回 0,但我想知道该列中存在多少空值,例如

SELECT COUNT (column) AS count 
FROM table 
WHERE column = 'some value';

其中returns条匹配记录数

您可以使用条件 sum()

SELECT sum(case when column is null 
                then 1 
                else 0 
           end) AS count 
FROM table

NULL 值的特殊之处在于您不能将 = 与它一起使用;您必须使用 IS NULL 代替:

SELECT COUNT (*) AS count FROM table where column IS null ;

这是因为 SQL 中的 NULL 不等于任何值,包括其他 NULL 值。还要注意使用 * 作为 COUNT.

的参数

一个不同的查询,但确切的答案检查一下

select count(*)-count(column) from table

如果对您有帮助请投票打勾

要获得准确的输出,您也可以使用以下命令 -

SELECT COUNT (*) AS count FROM table where column IS null OR column='';

因为有时只有 '' 不算作 NULL。