(mysql)一條sql統計一張表多個狀態的數量


第一種、通過count和if判斷來統計

select
COUNT(*) AS total,
COUNT(if( mdt_apply_type='1' ,1,NULL) )as singleDiseasesNum,
COUNT(if( mdt_apply_type='2' ,1,NULL) )as multidisciplinaryNum
from t_mdt_apply_list b WHERE 1=1 AND audit_opinion='3'

查詢效果如下:
在這里插入圖片描述
第二種、通過sum和if判斷來統計

select
COUNT(*) AS total,
SUM(if( mdt_apply_type='1' ,1,0) )as singleDiseasesNum,
SUM(if( mdt_apply_type='2' ,1,0) )as multidisciplinaryNum
from t_mdt_apply_list b WHERE 1=1 AND audit_opinion='3'

查詢效果如下:
在這里插入圖片描述


免責聲明!

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



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