mysql sum函数不适用于按分类id排序的多行

i5desfxk  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(340)

我想列出总投票差,公平,好,vgood,优秀,是,否从我的调查结果表,但查询只有一个时间循环。
这是调查结果表。

这是查询结果

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(result.poor) AS poor,
SUM(result.fair ) AS fair,
SUM(result.good) AS good,
SUM(result.vgood) AS vgood,
SUM(result.excellent) AS excellent,
SUM(result.yes) AS yes,
SUM(result.no) 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
3pvhb19x

3pvhb19x1#

我想从我的调查结果表中列出总票数:差、一般、好、好、优秀、是、否
听起来你想要整个调查的总数。如果是这样的话,你不应该使用 GROUP BY . 要获得一行:

SELECT SUM(result.poor) AS poor,
       SUM(sr.fair ) AS fair,
       SUM(sr.good) AS good,
       SUM(sr.vgood) AS vgood,
       SUM(sr.excellent) AS excellent,
       SUM(sr.yes) AS yes,
       SUM(sr.no) As no
FROM survey_categori sc INNER JOIN
     survey_questions sq
     ON sc.s_categori_id = sq.s_categori_id INNER JOIN
     survey_result sr
     ON sr.s_question_id = sq.survey_id
WHERE c.survey_type = 'class';

你会用 GROUP BY 如果您想在结果集中有多行。然后,在 GROUP BY 将(通常)添加到 SELECT . 例如,如果您希望每个调查有一行,则必须:

SELECT sq.survey_id,
       SUM(. . .)
       . . .
FROM . . .
GROUP BY sq.survey_id
9jyewag0

9jyewag02#

您必须在我在查询中添加的字段下面添加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,
SUM(result.poor) AS poor,
SUM(result.fair ) AS fair,
SUM(result.good) AS good,
SUM(result.vgood) AS vgood,
SUM(result.excellent) AS excellent,
SUM(result.yes) AS yes,
SUM(result.no) 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,categori.categori_name_en,
categori.categori_name_ar,
categori.categori_type,
question.question_en,
question.question_ar

相关问题