Pan*_*nar 5 mysql sql gaps-and-islands
我有mysql表名为user(id,name,join_on)join on是一个日期字段我想要的是每天显示已经创建了多少用途我可以使用group by但它只会给我用户获取的日期像日期一样添加
4/12/10 5 users added
4/13/10 2 users added
4/15/10 7 users added
Run Code Online (Sandbox Code Playgroud)
这里的日期4/14/10不见了,我希望在一个月内列出所有日期.我有一个解决方案,通过创建另一个表只用于添加日期,该表将在join_on上连接我的用户表并将给出总结果但我不想这样做,因为创建我需要创建和添加条目在日期表中,请建议采用不同的方法.
谢谢.
有一种方法可以在纯 SQL 中执行此操作,但它有局限性。
首先,您需要有一个数字序列 1,2,3...n 作为行(假设select row from rows返回该值)。
然后,您可以对此进行左连接,并根据最小和最大之间的天数转换为日期。
select @min_join_on := (select min(join_on) from user);
select @no_rows := (select datediff(max(join_on), @min_join_on) from user)+1;
Run Code Online (Sandbox Code Playgroud)
将为您提供所需的行数,然后您可以使用它
select adddate(@min_join_on, interval row day) from rows where row <= @no_rows;
Run Code Online (Sandbox Code Playgroud)
将返回所需的日期序列,然后您可以对用户表进行左连接。
如果您使用子查询,则可以避免使用变量,为了便于阅读,我将其分解。
现在的问题是表中的行数rows必须大于@no_rows。对于 10,000 行,您可以使用长达 27 年的日期范围,对于 100,000 行,您可以使用长达 273 年的日期范围(这感觉非常糟糕,但恐怕如果您不想使用存储过程它必须看起来和感觉很尴尬)。
因此,如果您可以使用此类固定日期范围,您甚至可以用查询替换表,例如
SELECT @row := @row + 1 as row FROM (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4, (SELECT @row:=0) r
Run Code Online (Sandbox Code Playgroud)
这将产生从 1 到 10,000 的 10,000 行,并且效率不会非常低。
所以最后它可以在单个查询中完成。
create table user(id INT NOT NULL AUTO_INCREMENT, name varchar(100), join_on date, PRIMARY KEY(id));
mysql> select * from user;
+----+-------+------------+
| id | name | join_on |
+----+-------+------------+
| 1 | user1 | 2010-04-02 |
| 2 | user2 | 2010-04-04 |
| 3 | user3 | 2010-04-08 |
| 4 | user4 | 2010-04-08 |
+----+-------+------------+
4 rows in set (0.00 sec)
insert into user values (null, 'user1', '2010-04-02'), (null, 'user2', '2010-04-04'), (null, 'user3', '2010-04-08'), (null, 'user4', '2010-04-08')
SELECT date, count(id)
FROM (
SELECT adddate((select min(join_on) from user), row-1) as date
FROM (
SELECT @row := @row + 1 as row FROM (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, (SELECT @row:=0) r ) n
WHERE n.row <= ( select datediff(max(join_on), min(join_on)) from user) + 1
) dr LEFT JOIN user u ON dr.date = u.join_on
GROUP BY dr.date
+------------+-----------+
| date | count(id) |
+------------+-----------+
| 2010-04-02 | 1 |
| 2010-04-03 | 0 |
| 2010-04-04 | 1 |
| 2010-04-05 | 0 |
| 2010-04-06 | 0 |
| 2010-04-07 | 0 |
| 2010-04-08 | 2 |
+------------+-----------+
7 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)