处理日期和时间跨度的好方法?

发布于 2024-09-28 16:40:54 字数 284 浏览 5 评论 0原文

对于我的网络应用程序,我需要处理访问请求。输入周一至周日时间表的请求,指定每天的时间。本周的日程最多可以重复 52 次。用户在一周内可以有多个计划(星期一 8-9 和星期一 10-11) 有以下要求:

  1. 可搜索/可过滤
  2. 检测重叠请求

我希望数据库能够处理尽可能多的提升。目前我能想到的唯一设计是将每天的访问存储为单独的记录。这样做时,我将提取用户的所有访问权限并循环以确定新请求是否重叠。这需要代码或存储过程。

有谁有更好的数据库模型想法或处理代码重叠的干净方法?

For my web app i need to handle access requests. Requests are entered for a monday-sunday schedule specifying hours for each day. This week schedule can repeat up to 52 times. A user can have multiple schedules during a week (mon 8-9 and mon 10-11) There are the following requirements:

  1. Searchable/filterable
  2. Detect overlapping requests

I want the database to handle as much of the lifting as possible. Right now the only design I can think of is storing each day's access as a separate record. Doing this I would pull all accesses for a user and loop to determine if the new request overlaps. This requires code or a stored procedure.

Does anyone have a better database model idea or a clean way to deal with overlaps in code?

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

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

发布评论

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

评论(1

枯叶蝶 2024-10-05 16:40:54

如果将每个访问请求存储在包含 start_time 和 end_time 字段的表中,则可以使用数据库的 BETWEEN 功能来确定特定访问是否会与数据库中已有的访问重叠。

例如,假设某人在周一上午 9:00 到中午 12:00 完成了访问请求。

然后其他人过来并尝试在周一上午 11:00 到下午 3:00 提出访问请求。要确定这是否会与其他内容冲突,您需要查找以下可能的条件:

  • start_time <周一上午 11:00结束时间>星期一 11:00AM
  • start_time BETWEEN 星期一 11:00AM 和 11:00AM星期一下午 3:00
  • 开始时间 == 星期一上午 11:00 || end_time == Monday 3:00PM

这些可以被转换为 SQL 查询,这将避免加载和迭代应用程序中的记录。作为奖励,您可以使用事务来防止竞争条件。

If you store each access request in a table with fields with start_time and end_time, then you could use the database's BETWEEN functionality to determine if a particular access would overlap with one already in the database.

As an example, let's say that someone had completed an access request for Monday from 9:00AM to 12:00PM.

Then someone else comes and tries to make an access request for Monday from 11:00AM to 3:00PM. To determine if this would conflict with something else, you'd need to look for the following possible conditions:

  • start_time < Monday 11:00AM && end_time > Monday 11:00AM
  • start_time BETWEEN Monday 11:00AM & Monday 3:00PM
  • start_time == Monday 11:00AM || end_time == Monday 3:00PM

These can bet translated down to a SQL query which would prevent having to load and iterate over records in the application. As a bonus, you can use transactions to guard against race conditions.

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