正如我在評論說:你應該有所有天表,左加入你的時間間隔,並使用SUM而不是COUNT。計數只會爲您桌子上存在的結果帶來結果。說你的間隔距離2013-01-01 to 2013-01-10
,你有theese登記:
Created
2013-01-01,
2013-01-01,
2013-01-02,
2013-01-03
它會導致:2, 1, 1
沒有辦法爲SQL知道應該存在01-04, 01-05, 01-06... 01-10
所以,你必須創建日期的數據並與表格左連接。事情是這樣的:
一些數據例如:
create table tbl_users(
created date
);
insert into tbl_users values
('2013-11-24'),
('2013-11-24'),
('2013-11-25'),
('2013-11-25'),
('2013-11-25'),
('2013-11-26'),
('2013-11-27'),
('2013-11-27'),
('2013-11-27'),
('2013-11-28'),
('2013-11-29');
create table alldays (
dateday date
);
--this is just an example to fit the result for today: 2013-11-28
--this table should have all days for the year
insert into alldays values
('2013-11-23'),
('2013-11-24'),
('2013-11-25'),
('2013-11-26'),
('2013-11-27'),
('2013-11-28'),
('2013-11-29'),
('2013-11-30'),
('2013-12-01'),
('2013-12-02'),
('2013-12-03');
您的查詢應該是:
SELECT a.dateday, sum(if(b.created is null, 0, 1))
FROM alldays a left join tbl_users b on (a.dateday=b.created)
WHERE a.dateday >= date_sub(curdate(), INTERVAL 7 DAY)
AND a.dateday <= NOW()
GROUP BY a.dateday
見這裏:http://sqlfiddle.com/#!2/cee122/7
做你想做的,你應該有一個表是什麼所有日子和左加入您的時間間隔,並使用SUM而不是COUNT。計數只會爲您桌子上存在的結果帶來結果。說你的時間間隔是從2013-01-01到2013-01-10,你有他們的註冊表:2013-01-01,2013-01-01,2013-01-02,2013-01-03它會導致:2,1,1沒有辦法讓sql知道應該存在01-04,01-05,01-06 ... 01-10 –
如果你能提供一個工作的例子,它將非常棒!我完全困惑在這一個:( –
看看我的回答 –