响应头表是一个联接表,它被计算为显示对特定调查的响应。如果没有行,因此没有响应,我希望计数显示0。但它将跳过行,因为在表中找不到联接参数(survey_id)。
SELECT DATE_FORMAT(launched_date,'<nobr>%e-%b-%Y %H:%i:%s</nobr>'),
survey.NAME,
survey.iris_type,
survey.launched_by,
COUNT(response_header_2010.survey_id) AS response_count,
survey.survey_id,
survey.NAME
FROM survey, response_header_2010
WHERE survey.STATUS='Live'
AND survey.iris_type!='Recipient List'
AND response_header_2010.survey_id = survey.survey_id
AND client_id = '98'
GROUP BY survey.survey_id, survey.NAME
ORDER BY response_count
最佳答案
您希望使用左联接,而不是当前的内部联接。
SELECT DATE_FORMAT(launched_date,'<nobr>%e-%b-%Y %H:%i:%s</nobr>'),
survey.NAME,
survey.iris_type,
survey.launched_by,
COUNT(response_header_2010.survey_id) AS response_count,
survey.survey_id,
survey.NAME
FROM survey
LEFT JOIN response_header_2010
ON survey.survey_id = response_header_2010.survey_id
WHERE survey.STATUS='Live'
AND survey.iris_type!='Recipient List'
AND client_id = '98'
GROUP BY survey.survey_id, survey.NAME
ORDER BY response_count
关于mysql - 计算联接表中的行,但如果不存在行则显示0-MySQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4686003/