Are there any MySQL functions to get all rows with with a start or end date that fall between a given start and end date?

前端 未结 5 2275
名媛妹妹
名媛妹妹 2020-12-06 12:48

I have a table of events with a recorded start and end time as a MySQL DATETIME object (in the format YYYY-MM-DD HH:MM:SS. I want to find all events that occur

5条回答
  •  旧巷少年郎
    2020-12-06 13:21

    Basically, you can use regular comparisons -- the ones above should work -- the trick is to check all the different cases that can occur.

    A) events with an ending date within the range

    B) events with a starting date within the range

    C) events with both starting and ending dates within the range

    D) events with both starting and ending dates outside the range, but overlapping it

    Robert's answer is a good one, but it doesn't take into account case D, where the event starts before the range and ends after the range.

提交回复
热议问题