这是数据库中表中的一些伪内容。
type approved
A true
A true
B true
C false
我想要得到的结果是:
A 2
B 1
C 0
计算每种类型获得批准的数量。但是如果我执行 SQL select count(approved) from table whereroved = true
,它只会显示
A 2
B 1
我怎样才能也显示C 0?非常感谢!
最佳答案
使用条件聚合。
select type, sum(case when approved = 'true' then 1 else 0 end) as total
from table
group by type
关于mysql - 如何对 SQL 中允许特定组计数为零的字段进行计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022658/