在给定两个小时的MySQL的情况下,选择在各行中拆分一行

发布于 2025-02-11 01:33:54 字数 2041 浏览 2 评论 0原文

你好社区你好吗? 假设我在表

ID启动Intertal1Interval2
112:00:0016:00:00555

中有此行并添加Interval1+Interval2直到字段结束。在此示例(12:00:00 +(55 + 5))中,它将返回

ID启动Internet1Interval2
112:00:0016:00:00555
113:00:00 16:0016:00:0055 55
114:00:0016:00:00555
115:00:0016:00:00555
116:00:0016:00:00555

我与MySQL相当新,而Ive挣扎了几个小时。 谢谢大家!

Hello community how are u doing.
Lets say i have this row in a table

idstartendinterval1interval2
112:00:0016:00:00555

So id like to create a view where i give the id as condition and it returns me rows from the field start and adding interval1+interval2 until the field end. In this example (12:00:00 + (55 + 5)) it would return

idstartendinterval1interval2
112:00:0016:00:00555
113:00:0016:00:00555
114:00:0016:00:00555
115:00:0016:00:00555
116:00:0016:00:00555

Im pretty new with mysql and ive been struggling for hours.
Thank u all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2025-02-18 01:33:54

对于MySQL V8,可以使用递归CTE -DB

with recursive cte(id, starttime,end_1,interval1,interval2) as 
(select * from table_1 union all
select id, addtime(starttime,SEC_TO_TIME((interval1 + interval2)*60)) starttime, end_1, interval1, interval2
from cte
where starttime < end_1)
select * from cte;

小提琴

For mysql v8, recursive CTE can be used -

with recursive cte(id, starttime,end_1,interval1,interval2) as 
(select * from table_1 union all
select id, addtime(starttime,SEC_TO_TIME((interval1 + interval2)*60)) starttime, end_1, interval1, interval2
from cte
where starttime < end_1)
select * from cte;

DB fiddle here.

你的他你的她 2025-02-18 01:33:54

假设在每个行Interval1和Interval2中可能具有不同的值,并且我们希望显示一个比一个ID的结果,甚至创建视图,仅横向>横向可以分别为每一行完成工作。它在8.0.14中添加。

create view periods as
select mytable.id, addtime(start,SEC_TO_TIME(a*(interval1 + interval2)*60)) starttime, end, interval1, interval2 from mytable, lateral (
with recursive cte as (
   select 0 as a 
   union all 
   select 1+a from cte where a+1<=time_to_sec(subtime(end,start))/((interval1+interval2)*60))
select * from cte
) b

之后

select * from periods where id = 3 order by starttime

Assuming, that in each row interval1 and interval2 may have different values, and we want to display results for more then one ID, or even create a view, only LATERAL can do the job for each row separately. It was added in 8.0.14.

create view periods as
select mytable.id, addtime(start,SEC_TO_TIME(a*(interval1 + interval2)*60)) starttime, end, interval1, interval2 from mytable, lateral (
with recursive cte as (
   select 0 as a 
   union all 
   select 1+a from cte where a+1<=time_to_sec(subtime(end,start))/((interval1+interval2)*60))
select * from cte
) b

and after that

select * from periods where id = 3 order by starttime

DB Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文