为什么未在COUNT(列名)中计算空值 [英] Why aren't nulls counted in COUNT(columnname)

查看:170
本文介绍了为什么未在COUNT(列名)中计算空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以前几天我在工作中遇到了一些问题,试图计算运行导入后有多少空值。

So I came across something the other day at work, trying to count how many null values after running an import.

所以,我做到了:

select COUNT(columnname) from table 
WHERE ColumnName is null

没有算空值...

然后我做了,

select COUNT(*) from table 
WHERE ColumnName is null

这是给我计数的。

让我烦恼的是为什么它不计算空值。

So, something that bugged me is why exactly this doesn't count the null values.

我已经看过这个问题(以及对Google的良好搜索...):
在SQL中,count(column)和count(*)有什么区别?
,虽然它告诉我 COUNT(列名)不算空值,但我想确切地知道为什么

I have looked at this question (along with a good search around Google...): In SQL, what's the difference between count(column) and count(*)?, and whilst it tells me that COUNT(columnname) doesn't count nulls, I would like to know exactly why nulls aren't counted using this method?

非常感谢,James

Many Thanks, James.

推荐答案

COUNT 对值进行计数,因为null不是一个值,因此不会被计数。

COUNT counts values, since null is not a value it does not get counted.

如果要计算所有空值,可以执行以下操作:

If you want to count all null values you could do something like this:

SELECT COUNT(ID) as NotNull, SUM(CASE WHEN ID IS NULL then 1 else 0 end) as NullCount

这篇关于为什么未在COUNT(列名)中计算空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆