(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