mysql sum如果返回多行

lmyy7pcs  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(434)

如果category.category\u type!='comment'返回comment null和sum poor,fair,good,vgood,vgood,excellent,yes,no列,否则返回该列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
nlejzf6q

nlejzf6q1#

尝试以下查询:带case when

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
wixjitnu

wixjitnu2#

在这种情况下,可以使用sqlcase语句
case语句是sql处理if/then逻辑的方法
每个case语句都必须以end语句结尾,其中else语句是可选的
就你而言:
如果条件为“1”,则应执行操作“1”,否则应执行操作“2”
case语句就是这样做的
当只有一个条件时,我们可以这样写

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语句上应用不同的聚合逻辑,每次迭代一个一个记录,用逗号(,)分隔,我们可以将它写成

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

因为我们正在应用聚合逻辑,所以需要确保列位于GROUPBY子句中
希望这对下次申请有帮助:)

djmepvbi

djmepvbi3#

正确使用when和groupby子句时的用例

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

相关问题