(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'

查询效果如下:
在这里插入图片描述

posted @ 2019-07-03 16:24  ourlang  阅读(2774)  评论(0编辑  收藏  举报