mysql运用sum函数进行一个字段多种结果统计

遇到问题是要统计某个列分别在为a,为b,为c的时候出现了多少次,一开始是想到用count()函数,但是count并不支持加条件,最后用sum()函数加条件解决的,希望可以帮到遇到类似问题的朋友们。sql如下

SELECT date_format(time,’%Y-%m-%d’) as aa,count(1),sum(CASE when a = 1 THEN 1 ELSE 0 end) as a,sum(CASE when a = 2 THEN 1 ELSE 0 end) as b,sum(CASE when a = 3 THEN 1 ELSE 0 end) as c FROM table WHERE a > 0 GROUP BY aa;

你可能感兴趣的:(数据库)