计算联接表中的行,但如果不存在行则显示 0 - mysql
Posted
技术标签:
【中文标题】计算联接表中的行,但如果不存在行则显示 0 - mysql【英文标题】:count rows in joined table but display 0 if no rows exist - mysql 【发布时间】:2011-01-13 22:21:26 【问题描述】:响应标题表是一个连接表,用于显示对特定调查的响应。如果没有行,因此没有响应,我希望计数显示为 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
【问题讨论】:
【参考方案1】:您想使用 LEFT JOIN 而不是您当前拥有的 INNER JOIN。
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
【讨论】:
以上是关于计算联接表中的行,但如果不存在行则显示 0 - mysql的主要内容,如果未能解决你的问题,请参考以下文章