MySQL check if two date range overlap with input

前端 未结 4 748
生来不讨喜
生来不讨喜 2021-02-04 20:32

I need to check if two dates over lap with another two dates in my database.

My database looks like this

+----+--------------+------------+------------+
         


        
4条回答
  •  暖寄归人
    2021-02-04 21:10

    You can cover all date overlapping cases even also when toDate 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.

提交回复
热议问题