类似的东西(你需要生成日期表):
SQLFIDDLE示例
select a.Date,
COALESCE((SELECT cnt
FROM Table1 t1
WHERE t1.date = a.Date), 0) as COUNT
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between '2013-02-01' and '2013-03-01'
ORDER BY a.Date
结果:
| DATE | COUNT |
-------------------------------------------
| February, 14 2013 00:00:00+0000 | 10 |
| February, 15 2013 00:00:00+0000 | 0 |
| February, 16 2013 00:00:00+0000 | 4 |
| February, 17 2013 00:00:00+0000 | 4 |
| February, 18 2013 00:00:00+0000 | 0 |
| February, 19 2013 00:00:00+0000 | 0 |
| February, 20 2013 00:00:00+0000 | 9 |
您的查询应如下所示:
select DATE_FORMAT(a.Date, "%D %b") AS date,
COALESCE((SELECT COUNT(id)
FROM registration
WHERE createdTimestamp = a.Date), 0) as COUNT
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between DATE_SUB(CURDATE(), INTERVAL 7 DAY) and CURDATE()
ORDER BY a.Date