一条sql查询出多个统计结果

统计一张表中不同类型的统计数量
例如,该表中有个type字段(值有0,1....),然后统计不同值下的总数

SELECT
COUNT(CASE WHEN `type`=0 OR `type`=1  THEN 1 ELSE NULL END) allTypeCount, 
COUNT(CASE WHEN `type`=0 THEN 1 ELSE NULL END) zerotTypeCount, 
COUNT(CASE WHEN `type`=1 THEN 1 ELSE NULL END) oneTypeCount
FROM `book_detail`
select 

sum(case month(start_time) when '1' then 1 else 0 end) as 一月份,
sum(case month(start_time) when '2' then 1 else 0 end) as 二月份,
sum(case month(start_time) when '3' then 1 else 0 end) as 三月份,
sum(case month(start_time) when '4' then 1 else 0 end) as 四月份,
sum(case month(start_time) when '5' then 1 else 0 end) as 五月份,
sum(case month(start_time) when '6' then 1 else 0 end) as 六月份,
sum(case month(start_time) when '7' then 1 else 0 end) as 七月份,
sum(case month(start_time) when '8' then 1 else 0 end) as 八月份,
sum(case month(start_time) when '9' then 1 else 0 end) as 九月份,
sum(case month(start_time) when '10' then 1 else 0 end) as 十月份,
sum(case month(start_time) when '11' then 1 else 0 end) as 十一月份,
sum(case month(start_time) when '12' then 1 else 0 end) as 十二月
 ,2023 AS '年份'
from 
pro_project  WHERE YEAR(start_time) = 2023 
posted @ 2022-05-09 15:02  寒冷的雨呢  阅读(907)  评论(0编辑  收藏  举报