方案一
select id, count(case when 屬性 in (1,2) then 1 else 0 end) as sum1, count(case when 屬性 in (3) then 1 else 0 end) as sum2, sum(case when 屬性 in (4,5) then 1 else 0 end) as sum3 from 表名 group by id
方案二
SELECT count( * ) AS num, count(if(字段=1,true,null)) AS sum1, count(if字段=2,true,null)) AS sum2 FROM 表名 WHERE 其他條件
當然也還有別的方法:
1、沉到業務中取處理,拆分SQL
2、抽取公用統計屬性,建立冗余表