Sia*_*ami 4 sql sqlite null group-by count
我有一个有2个字段的表
x y
---- ----
1 null
2 5
3 5
4 null
5 null
6 10
7 5
Run Code Online (Sandbox Code Playgroud)
我的SQLite查询是
select y,count(y)
from mytable
group by y
Run Code Online (Sandbox Code Playgroud)
结果是
null 0
5 3
10 1
Run Code Online (Sandbox Code Playgroud)
预计会看到null 3.
但输出为null 0.
这是什么意思?
him*_*056 14
count(X)函数返回组中X不为NULL的次数.count(*)函数(不带参数)返回组中的总行数.
所以,COUNT
函数不计算NULL
所以使用COUNT(*)
而不是COUNT(y)
.
SELECT y, COUNT(*) AS COUNT
FROM mytable
GROUP BY y
Run Code Online (Sandbox Code Playgroud)
或者你也可以COUNT(x)
像这样使用.
SELECT y, COUNT(x) AS COUNT
FROM mytable
GROUP BY y
Run Code Online (Sandbox Code Playgroud)