SELECT
DATE(`date`) AS RegistrationDate, COUNT(`id`) AS NumberOfRegistrations
FROM
Users
WHERE
`date` between "2018/05/1" and "2018/05/13"
GROUP BY
RegistrationDate
SELECT DATE(`date`) AS RegistrationDate, COUNT(u.id) AS NumberOfRegistrations
FROM Calendar c LEFT JOIN
Users u
ON DATE(u.date) = c.date
WHERE c.date >= '2018-05-01' AND c.date < '2018-05-14'
GROUP BY RegistrationDate;
SELECT DATE(`date`) AS RegistrationDate, COUNT(u.id) AS NumberOfRegistrations
FROM (SELECT DATE('2018-05-01') as dte UNION ALL
DATE('2018-05-02') as dte UNION ALL
DATE('2018-05-03') as dte UNION ALL
DATE('2018-05-04') as dte UNION ALL
DATE('2018-05-05') as dte UNION ALL
DATE('2018-05-06') as dte UNION ALL
DATE('2018-05-07') as dte UNION ALL
DATE('2018-05-08') as dte UNION ALL
DATE('2018-05-09') as dte UNION ALL
DATE('2018-05-10') as dte UNION ALL
DATE('2018-05-11') as dte UNION ALL
DATE('2018-05-12') as dte UNION ALL
DATE('2018-05-13') as dte UNION ALL
DATE('2018-05-14') as dte
) c LEFT JOIN
Users u
ON DATE(u.date) = c.dte
WHERE c.dte >= '2018-05-01' AND c.dte < '2018-05-14'
GROUP BY RegistrationDate;
笔记: 对日期yyyy-mm-dd使用标准日期格式。 sql中的标准字符串分隔符是单引号。使用它,除非你有充分的理由使用双引号。 不要使用 between 有日期。目前还不清楚您是否打算包括2018-05-13,但您的查询可能会排除该日期的任何数据(由于时间成分)。
SELECT
DATE(`gen_date`) AS RegistrationDate,
COUNT(`id`) AS NumberOfRegistrations
FROM
(
SELECT *
FROM
(SELECT adddate('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) gen_date
FROM
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
) v
WHERE
gen_date BETWEEN "2018-05-01" AND "2018-05-13"
) d
LEFT OUTER JOIN users u ON Date(`gen_date`) = Date(`date`)
GROUP BY
gen_date
2条答案
按热度按时间ltskdhd11#
如果您有一个日历表,那么您的问题就解决了:
一
Calendar
表可以是非常有用的,你可以找到一个谷歌搜索。你可以用一个
numbers
table,如果你手边有一张的话。如果没有这些,您可以生成所需的日期:
笔记:
对日期yyyy-mm-dd使用标准日期格式。
sql中的标准字符串分隔符是单引号。使用它,除非你有充分的理由使用双引号。
不要使用
between
有日期。目前还不清楚您是否打算包括2018-05-13,但您的查询可能会排除该日期的任何数据(由于时间成分)。pjngdqdw2#
在我的评论中,这是我处理你问题的方法。
(可能还有其他方法)
联接中的第一个表创建日期范围。
看到它在这里工作了吗