0
SELECT Count(Phq_2) as Phq_2, Date(created) as created
FROM `survey`
WHERE Phq_2 != ''
GROUP BY Date(created)
ORDER BY Date(created) ASC
によってグループ内に結果がこのはどこでできますが、日付のリストを作成することができさえ
期待される結果
SELECT Count(Phq_2) as Phq_2, Date(created) as created
FROM `survey`
WHERE Phq_2 != ''
GROUP BY Date(created)
ORDER BY Date(created) ASC
によってグループ内に結果がこのはどこでできますが、日付のリストを作成することができさえ
期待される結果
を表示しない場合は、間のすべての日付を表示しますあなたのテーブルにリストして参加するのに使います。以下のクエリのテーブルcが日付テーブルです。
SELECT c.date,
COUNT(s.phq_2)
FROM (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
) c
LEFT JOIN `survey` s
ON c.date = s.created
WHERE s.phq_2 != ''
AND c.date BETWEEN '2017-08-30' AND '2017-09-06'
GROUP BY c.date
ORDER BY c.date
結果
date COUNT(s.phq_2)
2017-08-30 1
2017-08-31 1
2017-09-01 0
2017-09-02 0
2017-09-03 0
2017-09-04 0
2017-09-05 1
2017-09-06 6
あなたは '2017年8月30日' AND '2017年9月6日BETWEEN' WHERE c.dateのための固定の日付をしたい場合、あなたは(結果MINを使用することができます)とサブテーブルのテーブルから作成された列のMAX()
のデータ表示の問題を処理することを検討してください。ちょうどこの1つを使用して、テーブルに左の結合を追加して、問題を解決してください。 –
興味のあるすべての日付とクエリのLEFT JOINを生成する一時テーブル[WITH]を作成する必要があります。私は大公の質問だと思う、私は何度もそれを見た! –
いくつかのアイデアはhttps://stackoverflow.com/questions/1046865/mysql-select-all-dates-in-a-range-even-if-no-records-present – EdmCoff
で解決してください質問は無駄です –