1

我有这样的查询

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
4

1 回答 1

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.

于 2012-11-08T12:22:56.693 回答