2
SELECT DATE_FORMAT(createdTimestamp, "%D %b") AS date,
       COUNT(id) AS COUNT
FROM registration
WHERE createdTimestamp BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdTimestamp)

它会显示类似

14th Feb - 10
15th Feb - 12
16th Feb - 4
17th Feb - 4
18th Feb - 10
19th Feb - 12
20th Feb - 9

但是如果有一天没有注册它会跳过这一天,我如何用mysql查询将它显示为0?

14th Feb - 10
16th Feb - 4
17th Feb - 4
20th Feb - 9

查询可以显示如下

14th Feb - 10
15th Feb - 0
16th Feb - 4
17th Feb - 4
18th Feb - 0
19th Feb - 0
20th Feb - 9
4

4 回答 4

4

类似的东西(你需要生成日期表):

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
于 2013-04-12T09:24:19.003 回答
2

此查询创建一个由 7 个数字组成的序列(从 0 到 6),并使用 curdate 和序列号之间的日期差异提取您需要的数据:

SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL t.DAY DAY), "%D %b") AS date,
   COUNT(r.id) AS COUNT
FROM
  (SELECT 0 AS 'day'
       UNION ALL SELECT 1
   UNION ALL SELECT 2
   UNION ALL SELECT 3
   UNION ALL SELECT 4
   UNION ALL SELECT 5
   UNION ALL SELECT 6) t
LEFT OUTER JOIN registration r ON DATE_SUB(CURDATE(), INTERVAL t.DAY DAY) = r.createdTimestamp
GROUP BY t.DAY

这是sqlfiddle的链接

于 2013-04-12T09:37:01.100 回答
1

您当前的查询是不可能的。查询不能按不存在的东西分组。您需要生成缺失的数据。

选择您要选择为表格的日期列表,然后将其按日期连接到您的数据,并按生成的日期表中的数据列分组。

编辑:您可以通过这种方式生成要加入的日期表(例如 2 天):

select * from (select subdate(current_date,0)) a union (select subdate(current_date,1) b);
于 2013-04-12T08:51:49.223 回答
1

要获取日期范围,您可以获取一个整数表(从 0 到 9)并将其与自身连接以获得所需的尽可能大的数字范围,然后使用 DATE_SUB 从中获取日期范围。

然后 LEFT OUTER JOIN 与您当前的表在日期上得到每个日期的计数。

类似于以下内容:-

SELECT DATE_FORMAT(aDate, "%D %b") AS fmtdate, COUNT(registration.id) AS COUNT
FROM (SELECT DATE_SUB(CURDATE(), INTERVAL a.i + b.i*10 DAY) AS aDate
FROM integers a, integers b
WHERE (a.i + b.i*10) <= 7) CountRange
LEFT OUTER JOIN registration
ON aDate = DATE(FROM_UNIXTIME(createdTimestamp))
GROUP BY aDate
ORDER BY aDate

(请注意,这最多可以处理 99 天 - 我对整数进行了连接只是为了演示如何扩展范围)

编辑 - 上面的代码使用 FROM_UNIXTIME,因为我假设您的时间戳存储为 unix 时间戳。如果我的假设不正确,很容易删除该函数调用

于 2013-04-12T09:31:58.760 回答