(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'
查询效果如下:
还没有评论,来说两句吧...