Find overlapping (date/time) rows within one table

后端 未结 4 1405
广开言路
广开言路 2021-01-30 12:13

I have a table which stores in each row a meeting with start date/time and end date/time.

meetingID int
meetingStart datetime
meetingEnd datetime

De

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 12:26

    Probably something like this:

    SELECT m1.meetingID, m2.meetingID
    FROM meeting AS m1, meeting AS m2
    WHERE m1.meetingID < m2.meetingID
        AND m1.meetingStart BETWEEN m2.meetingStart AND m2.meetingEnd
        OR m1.meetingEnd BETWEEN m2.meetingStart AND m2.meetingEnd
    

    By selecting only m1.meetingID < m2.meetingID you dont compare rows with itself and don't get duplicates because every row would be joined twice (m1, m2) and (m2, m1)

提交回复
热议问题