How to check if two date ranges overlap in mysql?

后端 未结 4 1528
死守一世寂寞
死守一世寂寞 2021-01-01 03:31

In mysql, how can I check if two date ranges overlap?

I have this:

Note: We have that p.date_started <= p.date_finished but dateA

4条回答
  •  余生分开走
    2021-01-01 04:02

    You can cover all date overlapping cases even when to-date in database can possibly be null as follows:

    SELECT * FROM `tableName` t
    WHERE t.`startDate` <= $toDate
    AND (t.`endDate` IS NULL OR t.`endDate` >= $startDate);
    

    This will return all records that overlaps with the new start/end dates in anyway.

提交回复
热议问题