mysql意外语法错误

时间:2015-03-11 04:31:39

标签: mysql mysql-workbench

所以我有以下查询(有三个表,因此连接)

SELECT AVG(survey_responses.survey_response), survey_responders.province, 
survey_questions.question 
FROM survey_responses 
INNER JOIN survey_responders on survey_responses.id = survey_responders.id
INNER JOIN survey_questions on survey_responders.id = survey_questions.id
GROUP BY survey_questions.question;

现在有六个问题和十个省份,但它只会列出五个问题和五个省份,为什么会这样做呢?

1 个答案:

答案 0 :(得分:0)

您的查询会输出一个包含五个问题和五个省的列表,而不是六个问题和十个省,因为某些ID无法找到它对应的表ID,或者某些字段与其他字段无关其他表格。如果要检索所有行,请使用LEFT JOIN

SELECT
      AVG(survey_responses.survey_response), 
      survey_responders.province, 
      survey_questions.question 
FROM survey_responses 
LEFT JOIN survey_responders on survey_responses.id = survey_responders.id
LEFT JOIN survey_questions on survey_responders.id = survey_questions.id
GROUP BY survey_questions.question;