각 테이블의 개수를 얻기 위해 여러 테이블을 쿼리하고 있으며 결과 테이블에 레코드가있을 때이 쿼리가 올바르게 작동합니다. 나는이 쿼리를 결과 테이블에서 찾을 수있는 레코드가 없거나 질문에 대한 결과가없는 경우 해당 값에 대해 각각의 질문과 대답으로 카운트 '0'을 표시해야하는 방식으로이 쿼리를 처리하려고합니다. 이 쿼리에서 내가 무엇을 바꾸어야합니까? 아니면 어떤 방법 으로든 알려주세요. 미리 감사드립니다.쿼리 결과 집합이 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 등에서 개수를 표시해야합니다. 이 COUNT(ifnull(r.other,0)) AS other
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(ifnull(r.other,0)) 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 a.rank
편집 또한
@SlowCoder이 제안, 결과 테이블에 LEFT JOIN
를 사용해보십시오 추가
질문이나 답변에 대한 결과 테이블에 0 개의 레코드가있을 가능성이있는 경우 결과 테이블을 'LEFT JOIN'해야합니다. – Slowcoder
@SlowCoder thats right, 나는 그것을 고려하지 않았다. ... –
@Slowcoder - 결과 테이블에 남겨 두었을 때,이 열은 r.textbox, r.textboxmulti 및 r.other의 수를 표시하지 않는다. 카운트 기초 questionid. –