29

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

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 值返回,它只是被跳过并返回至少有一个用户的第二天。我怎样才能得到类似(psuedo-response)的东西:

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

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

谢谢!

4

6 回答 6

19

我希望你能弄清楚其余的。

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 到 max(n3)*100+max(n2)*10+max(n1)

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

您可以通过限制来调整您的动态日历,例如,从您必须的 min(date) 到 now()。

于 2013-05-01T12:18:41.323 回答
6

最好这样做:

-- 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;
于 2015-06-30T14:36:01.760 回答
5

这个问题和我想的一样。通常,公认的答案似乎是您要么在应用程序逻辑中执行此操作(将您拥有的内容读入数组,然后遍历数组并创建缺失的日期),要么使用填充了您希望的日期的临时表加入。

于 2009-06-26T00:40:41.350 回答
1

对表进行右外部联接,称为 tblCalendar,它预先填充了您希望报告的日期。并加入日期字段。

保罗

于 2009-06-26T00:45:36.643 回答
1

查询是:

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    |
+------------+-----------+
于 2015-06-30T11:48:46.973 回答
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.numberIF(ISNULL(ds2.number),0,ds2.number)或类似的东西。

于 2009-06-26T01:18:46.390 回答