How to determine if a date range occurs any time within another date range?

前端 未结 5 1618
星月不相逢
星月不相逢 2021-02-02 04:07

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

5条回答
  •  礼貌的吻别
    2021-02-02 04:36

    You could write your condition like this:

    start_date <= week_end AND end_date >= week_start
    

    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.

提交回复
热议问题