我想从Survey_result表中列出差,公平,好,很好,很好,是,否的总投票,但查询仅一个时间循环。
这是Survey_result表。
这是查询结果
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
答案 0 :(得分:2)
您必须在我在查询中添加的字段下方添加分组依据
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
答案 1 :(得分:1)
我想从我的Survey_result表中列出差,公平,好,很好,很好,是,否的总投票
这听起来像您想要整个调查的总数。如果是这样,则不应使用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