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 2277
名媛妹妹
名媛妹妹 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:17

    This will find every event that is completely contained inside the range:

    SELECT * FROM table WHERE start_date BETWEEN start_of_range AND end_of_range 
                          AND stop_date  BETWEEN start_of_range AND end_of_range
    

    This will find any events where any part of the event overlaps any part of the range:

    SELECT * FROM table WHERE start_date <= end_of_range 
                          AND stop_date  >= start_of_range
    

提交回复
热议问题