我在存储过程中有一个查询来计算表中的一些记录:
SELECT
t_Supporter.supporterID,
COUNT(t_ticket.ticketID) AS totalTicket
FROM t_Supporter
INNER JOIN t_ticket
ON t_Supporter.supporterID = t_ticket.supporterID_FK
WHERE (t_Supporter.orgSectionID_FK = @orgID)
GROUP BY t_Supporter.supporterID
如果t_Supporter.supporterID
中t_ticket
没有记录,如何让我的查询返回零。
答案 0 :(得分:1)
您需要使用条件汇总将COUNT
更改为SUM
,将INNER JOIN
更改为LEFT OUTER
SELECT t_Supporter.supporterID, SUM(CASE WHEN t_ticket.ticketID IS NOT NULL THEN 1 ELSE 0 END) AS totalTicket
FROM t_Supporter LEFT OUTER JOIN
t_ticket ON t_Supporter.supporterID = t_ticket.supporterID_FK
WHERE (t_Supporter.orgSectionID_FK = @orgID)
GROUP BY t_Supporter.supporterID