mysql 测试时间是否在午夜以上的时间范围内

发布于 2024-09-06 22:32:42 字数 589 浏览 2 评论 0原文

我正在研究一个 mysql 查询,该查询将搜索每个条目都有一个时间跨度的数据库,以查看用户输入时间或时间跨度是否落在条目跨度内。

前用户可以输入“13:00”或“13:00-16:00”
数据库具有如下条目:

id    startTime    endTime  
1     08:00        12:00  
2     20:00        03:00  
3     14:00        01:00  
4     16:00        21:00  

针对一天内的时间跨度(startTime < endTime)进行搜索非常容易。问题是测试跨度何时跨越午夜(endTime < startTime)。对于此应用程序,这些值不能有日期附件(它们仅存储为时间),因此 timediff 等将不起作用。

更新:
时间跨度永远不会大于 24 小时,因此 startTime 为 1 和 endTime 为 3 将始终是 2 小时项目,而不是 26 小时项目。

我不使用日期的原因是这或多或少与工作时间有关(但不完全是那个用例)。跨度代表的项目有每天的时间,因此为每天添加日期时间戳是不切实际的。

I am working on a mysql query that will search a database of entries that each have a time span to see if a user input time or time span falls within the entries spans.

Ex user can input "13:00" or "13:00-16:00"
the database has entries like so:

id    startTime    endTime  
1     08:00        12:00  
2     20:00        03:00  
3     14:00        01:00  
4     16:00        21:00  

Searching is easy enough against a time span that is during a single day (startTime < endTime). The issue is testing when a span goes across midnight (endTime < startTime). For this application these values can not have a date attachment (they are stored as time only), so timediff etc will not work.

UPDATE:
The time spans will never be greater than 24 hrs so startTime of 1 and endTime of 3 will always be a 2 hr item, not a 26 hr item.

The reason I am not using dates is this relates to something more or less like business hours (but not exactly that use case). The items the spans represent have daily hours, so it is not practical to add datetime stamps for every day.

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

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

发布评论

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

评论(1

断舍离 2024-09-13 22:32:42

我会检查是否结束时间

  1. 总时间 = 开始时间 + (24 小时 - 结束时间)

我看到的问题是结束时间是否是第二天并且结束时间>开始时间。例如,您从中午开始,到第二天下午 1 点结束。如果时间跨度超过2天,也会出现问题。例如,从第 1 天开始并在第 3 天或更长时间结束。我建议使用日期。

I would check if endTime

  1. totaltime = startTime + (24 hours - endTime)

The problem I see with this is if the endTime is the next day and endTime>startTime. For example you start at noon and end at 1pm the next day. There is also a problem if the time spans more than 2 days. For example start on day 1 and end day 3 or longer. I would recommend using dates.

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