在同一個sql語句中,統計不同條件的Count數量


方案一

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、抽取公用統計屬性,建立冗余表

 

showtooltip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM