我的應用程序需要一個線圖,我將顯示該國家在過去3天內加入的新用戶數量。我在同一個圖上繪製多條線。所以,我還需要顯示空值。打印零如果沒有返回COUNT()
用戶表:
+------------+-------------------+----------------------------------------+ |id | First_name |country_id | created_at | +------------+-------------------+-----------------+----------------------+ | 1 | AAA | 3 | 2014-02-23 15:55:55 | | 2 | BBB | 5 | 2014-02-22 15:55:55 | | 3 | CCC | 1 | 2014-02-22 17:55:55 | | 4 | DDD | 2 | 2014-02-22 15:55:55 | | 5 | EEE | 1 | 2014-02-22 16:55:55 | | 6 | FFF | 1 | 2014-02-23 15:55:55 | +------------+-------------------+-----------------+----------------------+
查詢:
Select COUNT(users.id) AS count, DATE(users.created_at) AS date , users.country_id
from `users`
where `created_at` >= '2014-02-21' and `created_at` < '2014-02-24' and users.country_id IN(1, 3, 10)
group by `date`, users.country_id
order by `date` asc
預期輸出:
+------------+-------------------+------------------ |count | date |country_id | +------------+-------------------+-----------------+ | 0 | 2014-02-21 | 1 | | 0 | 2014-02-21 | 3 | | 0 | 2014-02-21 | 10 | | 2 | 2014-02-22 | 1 | | 0 | 2014-02-22 | 3 | | 0 | 2014-02-22 | 10 | | 1 | 2014-02-23 | 1 | | 1 | 2014-02-23 | 3 | | 0 | 2014-02-23 | 10 | +------------+-------------------+-----------------+
上述闕如果沒有數據,則不會返回任何值。如果一天中沒有找到一個國家的數據,我該如何打印0。
爲什麼你對國家ID 2和5(它們在數據中表示)的結果不感興趣? –
在預期輸出你在表 –
10作爲COUNTRY_ID而不是'選擇從那裏created_at =「2014年2月21日」和COUNTRY_ID = 10'返回任何行用戶*? –