我正在查询几个表以获取每个答案的计数,当在结果表中找到记录时,此查询工作正常。我想以一种方式处理此查询,以防万一在结果表中找不到任何答案或问题的记录,它必须显示该值的计数“0”及其各自的问题和答案。我必须在这个查询中做出什么改变?或者有什么办法让我知道吗?提前致谢。
这是我的查询。
SELECT s.NAME AS surveyname,
COUNT(r.answer_id) AS totalAnswer,
q.id AS questionid,
q.question AS question,
a.answer AS answer,
COUNT(r.textbox) AS totalTextbox,
COUNT(r.textboxmulti) AS totalTextboxmulti,
qt.template AS template,
s.NAME AS surveyname,
COUNT(r.other) AS other
FROM surveys s
INNER JOIN survey_results AS sr
ON s.id = sr.survey_id
INNER JOIN results AS r
ON sr.id = r.surveyresults_id
INNER JOIN questions AS q
ON r.question_id = q.id
INNER JOIN questiontypes AS qt
ON q.questiontype_id = qt.id
LEFT JOIN answers AS a
ON r.answer_id = a.id
WHERE s.id = < cfqueryparam cfsqltype = "cf_sql_integer" value = "#arguments.surveyid#" >
GROUP BY q.id,
a.id
ORDER BY q.id, a.id a.rank
如果没有找到匹配的记录,它应该显示如下所示。
surveyname totalanswer qid question answer totaltextbox totaltextboxmulti template totalother
somename 0 1 what ? yes 0 0 multiple 0
somename 0 1 what ? no 0 0 multiple 0
如果有匹配的记录,那么它应该在 totalanswer 或 totaltextbox 等中显示计数。