Tags mySQL分组两次,先按省份分组,然后按问题类型分组,平均
我正在研究的问题是: “请先按省份再按问题列出问题的平均答案。您必须包括省份、问题以及平均答案。” 示例输出的内容如下所示:Tags mySQL分组两次,先按省份分组,然后按问题类型分组,平均,tags,Tags,我正在研究的问题是: “请先按省份再按问题列出问题的平均答案。您必须包括省份、问题以及平均答案。” 示例输出的内容如下所示: Ontario - Do you like blak? - 3.4 Ontario - Do you like flah? - 2.3 Ontario - Do you like rahhggg? - 4.8 Alberta - Do you like flah? - 2.3 Alberta - Do you like rahhggg? - 4.8 到目前为止
Ontario - Do you like blak? - 3.4
Ontario - Do you like flah? - 2.3
Ontario - Do you like rahhggg? - 4.8
Alberta - Do you like flah? - 2.3
Alberta - Do you like rahhggg? - 4.8
到目前为止,我试着把他们按平均数按问题列出,但我不知道如何先按省份分组。我认为这是一个连接,但我不知道如何安排它。我所拥有的:
SELECT survey_question_id, AVG(survey_response)
FROM survey_responses
JOIN survey_responders
ON survey_responses.id = survey_responders.id
WHERE survey_responders.province='Ontario'
GROUP BY survey_question_id;
如果需要所有省份,只需像这样删除Where
SELECT province,survey_question_id, AVG(survey_response)
FROM survey_responses
JOIN survey_responders
ON survey_responses.id = survey_responders.id
GROUP BY province,survey_question_id;
MySQL中的GROUPBY语句确定查询引擎如何聚合数据。您当前仅按调查\问题\ id进行分组。由于id都不同,您将永远无法在结果中获得聚合 我建议将您的小组改为:
GROUP BY survey_responders.province, survey_responses.questiontitle
并确保适当编辑SELECT语句。您还需要将
省
添加到SELECT子句中。修复了此问题,谢谢@jnevili将各省分组,就像有许多不同的地方一样。这个解决方案只适用于安大略省吗?是的,但是您只需要删除WHERE survey\u responders.province='antario'
clauseoh,我现在就知道了,谢谢:)
GROUP BY survey_responders.province, survey_responses.questiontitle