mysql - 如何对 SQL 中允许特定组计数为零的字段进行计数?

标签 mysql sql postgresql

这是数据库中表中的一些伪内容。

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/

相关文章:

mysql - Zend 框架 : Select query

MySQL 合并两个表的查询问题

sql - 在 Entity Framework 中的 SQL View 上使用外键

postgresql - 在 Slick 3.2.1 (Scala) 中使用过滤器的 where 子句中的 LIKE

mysql - sql - PostgreSQL 9.6 中的 GROUP_CONCAT

php - Xampp MySQL 崩溃并且不再工作

php - Laravel 5.5 - 如何抑制 MySQL 数据库截断警告消息

java - 线程 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 1 >= 1 中的异常

php - Postgres,查询错误

postgresql - 如何检查 PostgreSQL 事务中的挂起操作