使用sql查询返回0而不是什么

时间:2013-07-03 16:51:22

标签: sql sql-server-2008-r2

如果count为0,如何使用PossibleAnswerText作为响应返回0?现在它不会返回任何东西。

select 
    COUNT(sr.Id) AS 'Responses', qpa.PossibleAnswerText
from 
    CaresPlusParticipantSurvey.QuestionPossibleAnswer as qpa 
join  
    CaresPlusParticipantSurvey.SurveyResponse as sr on sr.QuestionPossibleAnswerId = qpa.Id
where 
    sr.QuestionPossibleAnswerId = 116 
GROUP BY 
    qpa.PossibleAnswerText

1 个答案:

答案 0 :(得分:1)

您需要使用LEFT JOIN,并从WHERE子句中删除“最右侧”表格中的条件;

SELECT COUNT(sr.Id) AS 'Responses', qpa.PossibleAnswerText
FROM CaresPlusParticipantSurvey.QuestionPossibleAnswer AS qpa 
LEFT JOIN CaresPlusParticipantSurvey.SurveyResponse as sr 
  ON sr.QuestionPossibleAnswerId = qpa.Id
WHERE qpa.Id = 116 
GROUP BY qpa.PossibleAnswerText