(mysql)一条sql统计一张表多个状态的数量

淩亂°似流年 2021-12-14 14:27 610阅读 0赞

第一种、通过count和if判断来统计

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

查询效果如下:
在这里插入图片描述
第二种、通过sum和if判断来统计

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

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

发表评论

表情:
评论列表 (有 0 条评论,610人围观)

还没有评论,来说两句吧...

相关阅读