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 に答える