What is a simple and efficient way to find rows with time-interval overlaps in SQL?

后端 未结 5 1557
清歌不尽
清歌不尽 2020-11-30 00:22

I have two tables, both with start time and end time fields. I need to find, for each row in the first table, all of the rows in the second table where the time intervals in

5条回答
  •  被撕碎了的回忆
    2020-11-30 00:49

    SELECT * 
    FROM table1,table2 
    WHERE table2.start <= table1.end 
    AND (table2.end IS NULL OR table2.end >= table1.start)
    

提交回复
热议问题