繁体   English   中英

MYSQL SUM IF返回多行

[英]MYSQL SUM IF return multiple rows

如果categori.categori_type!='comment'返回注释null,并且SUM差,公平,良好,vgood,vgood,优秀,是,没有列,则没有任何选择,否则返回此列0。我有1条以上的注释,但返回只有1条评论。

 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

用例when和group by子句正确

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

在这种情况下,您可以使用SQL CASE STATEMENT

CASE语句是SQL处理if / then逻辑的方式

每个CASE语句必须以END语句结尾,其中“ ELSE”语句是可选的

在您的情况下:

如果Condition_1,则发生Action_1,否则Action_2发生结束

这正是“案例陈述”所做的

当有单一条件时,我们可以写如下

Select CASE
       WHEN condition_1 THEN Action_1 Else Action_2 
       END AS <ALIAS_COLUMN_NAME>

如果要满足多个条件,我们可以写

Select CASE
       WHEN condition_1 THEN Action_1 Else Action_2  
       WHEN condition_2 THEN Action_3 Else Action_2   
       WHEN condition_3 THEN Action_4 Else Action_2
       END AS <ALIAS_COLUMN_NAME>

如果我们需要对case语句的不同迭代逻辑应用一次,每次迭代由comma(,)分隔开,一个记录一个记录,则可以将其写为

Select FUNCTION_1(CASE
       WHEN condition_1 THEN Action_1 Else Action_2  
       END) AS <ALIAS_COLUMN_NAME>,
       FUNCTION_1(CASE
       WHEN condition_2 THEN Action_1 Else Action_2  
       END) AS <ALIAS_COLUMN_NAME>,
       FUNCTION_1(CASE
       WHEN condition_3 THEN Action_1 Else Action_2  
       END) AS <ALIAS_COLUMN_NAME>
GROUP BY column1_in_condition_1

由于我们正在应用聚合逻辑,因此我们需要确保该列在group by子句中

希望这对下次申请有帮助:)

尝试以下查询:

    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,

     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 categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
 categori.categori_type,question.survey_id,question.question_en,
 question.question_ar,result.comment

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM