如何确定一个日期范围是否出现在另一个日期范围内的任何时间?
我有一个事件表,它指定带有 start_date
和 end_date
字段的日期范围。 我有另一个在代码中指定的日期范围,它将当前周定义为“week_start”和“week_end”。
我想查询本周的所有活动。 情况似乎是:
- 事件在一周内开始并结束
- 事件在一周之前开始,但在一周内结束
- 事件在一周内开始,但在一周之后结束
- 事件在一周之前开始,也在一周之后结束
- 事件既不在一周之内,也不与一周重叠的部分将被忽略
我正在尝试提出一个可以处理所有这些情况的查询。 到目前为止,我只能得到处理每周重叠的案例,或者完全内部的事件; 本质上,记录太多,或者根本没有。
I have an Event table that specifies a date range with start_date
and end_date
fields. I have another date range, specified in code, that defines the current week as 'week_start' and 'week_end'.
I'd like to query all Events for the week. The cases seem to be:
- Event begins and ends within the week
- Event begins before the week, but ends within the week
- Event begins within the week, but ends after the week
- Event begins before the week and also ends after the week
- Events that neither reside within, nor overlap the week at all are ignored
I'm attempting to come up with a query that can handle all these cases. So far I've only been able to get cases that handle the week overlaps, or events that are fully internal; Essentially, too many records, or none at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单来说,要么一周在事件期间开始,要么事件在一周内开始。
让我们检查一下:
活动在一周内开始。
本周在活动期间开始。
活动在一周内开始。
周在事件期间开始。
请注意,上面表达式中的
BETWEEN
只是为了简洁起见。严格的表达式如下所示:
,前提是
week.end
是week.start + INTERVAL 7 DAY
。IE。 如果您的一周从
周日 0:00:00
开始,那么它应该在下一个周日 0:00:00
结束(而不是在周六 0:00: 00:00
)这个表达式看起来比常用的表达式更复杂:
,但前者更高效且索引友好。
请参阅我的博客中的这些文章以进行性能比较:
In simple words, either a week starts during the event, or an event starts during the week.
Let's check it:
The event starts during the week.
The week starts during the event.
The event starts during the week.
The week starts during the event.
Note that
BETWEEN
in expressions above is used just for the sake of brevity.Strict expression looks like this:
, provided that
week.end
is aweek.start + INTERVAL 7 DAY
.I. e. if you week starts of
Sun, 0:00:00
, then it should end onnext Sun, 0:00:00
(not onSat, 0:00:00
)This expression looks more complex than the one which is commonly used:
, but the former is more efficient and index friendly.
See these articles in my blog for performance comparisons:
您可以这样写您的条件:
编辑:这假设 start_date <= end_date 和 week_start <= week_end (正确排序),并且由于不使用 OR (在某些数据库上可能会创建性能问题)
Edit2:此解决方案还解决了事件在间隔之前开始并在间隔之后结束的问题。
You could write your condition like this:
Edit: this assumes start_date <= end_date and week_start <= week_end ( are properly ordered) and gives you the best performance on most db implementations due to not using OR (which on some databases may create performance issues)
Edit2: this solution also solves the problem of events that begin before the interval and end after the interval.
为流行卡塔林+1,但可惜我没有投票权。
您想要的限制条件只是表达 Allen 的“OVERLAPS”运算符的标准方式。
其他 SQL 警告:如果 end_date 可为空,请务必将这些列中的空值视为“时间结束”。
附加功能警告:请务必调整“<=”与“<”的用法 记录的时间段是否包括结束日期。
+1 for pop Catalin, but alas I have no voting privilege.
The restrict condition you want is just the standard way to express Allen's "OVERLAPS" operator.
Additional SQL caveat : if end_date is nullable, be sure to treat nulls in those columns as "the end of time".
Additional functional caveat : be sure to adapt the usage of '<=' versus '<' to whether or not the recorded time periods include the end date or not.
为了...
In order...
(结束2>=开始1)&& (start2 <= end1) 我认为对于任何相交的日期范围都会返回 true。
我发现此处对此很有用的讨论。
(end2 >= start1) && (start2 <= end1) I think would return true for any intersecting date ranges.
I found a discussion about this here that I found useful.