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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图