gpt4 book ai didi

php - Mysql 将时间分割为查询中的时间段

转载 作者:行者123 更新时间:2023-11-29 23:16:21 42 4
gpt4 key购买 nike

我有一个包含可用时间的 MySQL 表:

ID  from   till   day
1 540 720 1
2 780 960 1
...

起始时间和终止时间只是从午夜算起的分钟数,例如上午 9 点 = 9*60 = 540 等等。

不同提供商的起始截止时间可能会有所不同,而且时间段也可能不同。

我需要做的是进行一个查询,从该表中选择结果,并将其拆分为多个时间段。

例如,一个时间段为 60 分钟,结果应如下所示:

from   till   day
540 600 1
600 660 1
660 720 1
780 840 1
...

你知道如何在 MySQL 中做到这一点吗?

提前致谢。

最佳答案

我会创建一个包含一天中所有时段的表。它看起来像这样:

CREATE TABLE time_slots (
time_slot_id INT(11) AUTO_INCREMENT,
start_time INT(11),
end_time INT(11)
);

您的查询将如下所示:

SELECT   ts.start_time, ts.end_time, avt.day

FROM available_times AS avt

JOIN time_slots ts
ON avt.from <= ts.end_time AND avt.till >= ts.start_time

ORDER BY ts.start_time;

关于php - Mysql 将时间分割为查询中的时间段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27761455/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com