我有这样的查询
select `temp`, IfNULL(count(id),0) t
from survey
where `temp`<>'NULL' and `temp`<> '' and date(submitdate)='date'
group by `temp
与o / p
temp t
A1 1
但是如果没有记录,这就是所选日期会发生什么:我没有得到任何结果。答案如A1,A2,A3
我需要这样的o / p
temp t
A1 1
A2 0
A3 0
答案 0 :(得分:1)
你必须做一些自我JOIN
技巧来获得每个答案的记录:
SELECT s.`temp`, count(survey.id) t
-- This will ensure that you will get all relevant answers A1, A2, A3
FROM (
SELECT DISTINCT `temp`
FROM survey
WHERE `temp` <> 'NULL' and `temp` <> ''
-- ^^^^^^^^^ Might want to replace this by `temp` IS NOT NULL?
) s
-- Only then, you will actually get the actual records per answer and date
LEFT OUTER JOIN survey
ON s.`temp` = survey.`temp`
AND date(survey.submitdate) = 'date'
GROUP BY s.`temp`
请确保您在survey.temp
上有索引。