MySQL:即使没有记录,也选择范围内的所有日期

时间:2009-06-26 00:35:17

标签: mysql date gaps-and-islands

我有一个用户数据库。我想基于用户群增长创建一个图表。我现在的查询是:

SELECT DATE(datecreated), count(*) AS number FROM users 
WHERE DATE(datecreated) > '2009-06-21' AND DATE(datecreated) <= DATE(NOW())
GROUP BY DATE(datecreated) ORDER BY datecreated ASC

这会使几乎返回我想要的内容。如果我们有一天获得0个用户,则该日期不会返回为0值,只会跳过该值,并且会返回至少有一个用户的第二天。我怎样才能得到类似的东西(伪造的反应):

date1 5
date2 8
date3 0
date4 0
date5 9
etc...

其中零日期按顺序显示其余日期?

谢谢!

6 个答案:

答案 0 :(得分:15)

我希望你能弄清楚其余部分。

select  * from (
select date_add('2003-01-01 00:00:00.000', INTERVAL n5.num*10000+n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) as date from
(select 0 as num
   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) n1,
(select 0 as num
   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) n2,
(select 0 as num
   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) n3,
(select 0 as num
   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) n4,
(select 0 as num
   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) n5
) a
where date >'2011-01-02 00:00:00.000' and date < NOW()
order by date

select n3.num*100+n2.num*10+n1.num as date

你会得到一个数字从0到最大(n3)* 100 +最大(n2)* 10 +最大(n1)的列

由于这里我们将max n3设为3,SELECT将返回399,加上0 - &gt; 400条记录(日历中的日期)。

您可以通过限制动态日历来调整动态日历,例如,从现在的最小(日期)()开始。

答案 1 :(得分:5)

This question问我想的是同样的事情。一般来说,接受的答案似乎是你要么在你的应用程序逻辑中读取(读入你在数组中的内容,然后遍历数组并创建缺少的日期),或者使用填充了你希望的日期的临时表加入。

答案 2 :(得分:5)

这样做更好:

-- 7 Days:
set @n:=date(now() + interval 1 day);
SELECT qb.day_series as days , COALESCE(col_byte, 0) as Bytes from tbl1 qa
    right join (
        select (select @n:= @n - interval 1 day) day_series from tbl1 limit 7 ) as qb 
    on date(qa.Timestamp) = qb.day_series and 
qa.Timestamp > DATE_SUB(curdate(), INTERVAL 7 day) order by qb.day_series asc

-- 30 Days:
set @n:=date(now() + interval 1 day);
SELECT qb.day_series as days , COALESCE(col_byte, 0) as Bytes from tbl1 qa
    right join (
        select (select @n:= @n - interval 1 day) day_series from tbl1 limit 30 ) as qb 
    on date(qa.Timestamp) = qb.day_series and 
qa.Timestamp > DATE_SUB(curdate(), INTERVAL 30 day) order by qb.day_series asc;

或没有这样的变量:

SELECT qb.day_series as days , COALESCE(col_byte, 0) as Bytes from tbl1 qa
right join (
    select curdate() - INTERVAL a.a day as day_series 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
    ) as a ) as qb
on date(qa.Timestamp) = qb.day_series and
qa.Timestamp > DATE_SUB(curdate(), INTERVAL 7 day) order by qb.day_series asc;

答案 3 :(得分:1)

对表执行右外连接,将其称为tblCalendar,预先填充您要报告的日期。并加入日期字段。

答案 4 :(得分:0)

进一步思考,这样的事情应该是你想要的:

CREATE TEMPORARY TABLE DateSummary1 ( datenew timestamp ) SELECT DISTINCT(DATE(datecreated)) as datenew FROM users;

CREATE TEMPORARY TABLE DateSummary2 ( datenew timestamp, number int ) SELECT DATE(datecreated) as datenew, count(*) AS number FROM users 
WHERE DATE(datecreated) > '2009-06-21' AND DATE(datecreated) <= DATE(NOW())
GROUP BY DATE(datecreated) ORDER BY datecreated ASC;

SELECT ds1.datenew,ds2.number FROM DateSummary1 ds1 LEFT JOIN DateSummary2 ds2 on ds1.datenew=ds2.datenew;

这将为您提供第一个表中的所有日期,以及第二个表中的count摘要数据。您可能需要将ds2.number替换为IF(ISNULL(ds2.number),0,ds2.number)或类似内容。

答案 5 :(得分:0)

查询是:

SELECT qb.dy as yourday, COALESCE(count(yourcolumn), 0) as yourcount from yourtable qa 
right join (
    select curdate() as dy    union
    select DATE_SUB(curdate(), INTERVAL 1 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 2 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 3 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 4 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 5 day) as dy     union
    select DATE_SUB(curdate(), INTERVAL 6 day) as dy        
    ) as qb 
on qa.dates = qb.dy 
and qa.dates > DATE_SUB(curdate(), INTERVAL 7 day)
order by qb.dy asc;

结果是:

+------------+-----------+
| yourday    | yourcount |
+------------+-----------+
| 2015-06-24 | 274339    |
| 2015-06-25 |      0    |
| 2015-06-26 |      0    |
| 2015-06-27 |      0    |
| 2015-06-28 | 134703    |
| 2015-06-29 |  87613    |
| 2015-06-30 |      0    |
+------------+-----------+