doulu6929 2018-08-13 06:48
浏览 270
已采纳

MYSQL SUM IF返回多行

Is there any option if categori.categori_type != 'comment' return comment null and SUM poor,fair,good,vgood,vgood,excellent,yes,no column otherwise return this columns 0. I have more than 1 comment but it s return only 1 comment.

 SELECT 
 categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
 categori.categori_type,question.survey_id,question.question_en,
 question.question_ar,
 IF(categori.categori_type != 'comment',SUM(result.poor),0) AS poor,
 IF(categori.categori_type != 'comment',SUM(result.fair),0) AS fair,
 IF(categori.categori_type != 'comment',SUM(result.good),0) AS good,
 IF(categori.categori_type != 'comment',SUM(result.vgood),0) AS vgood,
 IF(categori.categori_type != 'comment',SUM(result.excellent),0) AS 
 excellent,
 IF(categori.categori_type != 'comment',SUM(result.yes),0) AS yes,
 IF(categori.categori_type != 'comment',SUM(result.no),0) As no,
 result.comment 
 FROM survey_categori AS categori 
 INNER JOIN survey_questions AS question
 ON categori.s_categori_id = question.s_categori_id 
 INNER JOIN survey_result AS result 
 ON result.s_question_id = question.survey_id 
 WHERE categori.survey_type = 'class'
 GROUP BY question.survey_id
  • 写回答

3条回答 默认 最新

  • dongpin2969 2018-08-13 06:56
    关注

    use case when and group by clause properly

    SELECT 
         categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
         categori.categori_type,question.survey_id,question.question_en,
         question.question_ar,
        sum(case when categori.categori_type != 'comment' then result.poor else 0 end) as poor,
        sum(case when categori.categori_type != 'comment' then result.fair else 0 end) as fair,
        sum(case when categori.categori_type != 'comment' then result.good else 0 end) as good,
         sum(case when categori.categori_type != 'comment' then result.vgood else 0 end) as vgood,
         sum(case when categori.categori_type != 'comment' then result.excellent else 0 end) as excellent,
         sum(case when categori.categori_type != 'comment' then result.yes else 0 end) as yes,
         sum(case when categori.categori_type != 'comment' then result.no else 0 end) as no,
    
         case when categori.categori_type = 'comment' then result.comment   end as rcomment
         FROM survey_categori AS categori 
         INNER JOIN survey_questions AS question
         ON categori.s_categori_id = question.s_categori_id 
         INNER JOIN survey_result AS result 
         ON result.s_question_id = question.survey_id 
         WHERE categori.survey_type = 'class'
         GROUP BY categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
         categori.categori_type,question.survey_id,question.question_en,
         question.question_ar,rcomment
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法