[英]Generate time serie between time interval with steps of minutes on the fly in SQL
The question is in the end of this post.问题在这篇文章的最后。
I found this link to generate date Generating a series of dates and modified it to generate time in an intervall:我发现这个链接生成日期生成一系列日期并修改它以生成时间间隔:
SET @start_time = "08:02";
SET @stop_time = "17:02";
SELECT
TIME_FORMAT(time(CONCAT(m3, m2, ':', m1, m0)) , "%H:%i") as Time
FROM
(SELECT 0 m0 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) AS m0,
(SELECT 0 m1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) AS m1,
(SELECT 0 m2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS m2,
(SELECT 0 m3 UNION SELECT 1 UNION SELECT 2) AS m3
where time(CONCAT(m3, m2, ':', m1, m0)) is not null
and time(CONCAT(m3, m2, ':', m1, m0)) >= @start_time
and time(CONCAT(m3, m2, ':', m1, m0)) <= @stop_time
order by Time asc
This will generate this table:这将生成此表:
Time
10:02
10:03
10:04
10:05
10:06
...
16:59
17:00
17:01
Can this be written in a more effective way?这可以用更有效的方式编写吗? I should like to specify an interval with steps, so can quickly update it to every X minute intervall instead.
我想指定一个带有步骤的间隔,因此可以快速将其更新为每 X 分钟间隔。 I don't want to store the result in DB, i just want to generate it on the fly.
我不想将结果存储在数据库中,我只想即时生成它。 Since I will join it with other tables.
因为我将把它与其他表连接起来。
I use this a lot in mysql queries to make time serie for measurements that are easy to plot in Excel.我在 mysql 查询中经常使用它来制作易于在 Excel 中绘制的测量值的时间序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.