开发者

count rows in joined table but display 0 if no rows exist - mysql

The response header table is a joined table which is counted to display responses to a particular survey. If there are no rows, hence no responses, I want the count to display 0. But instead it is skipping the row because the join parm (survey_id) is not being found in the table.

SELECT DATE_FORMAT(launc开发者_如何学Gohed_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


You want to use a LEFT JOIN instead of the INNER JOIN you currently have.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜