我的应用程序需要一个线图,我将显示该国家在过去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'返回任何行用户*? –