MySQL-查找具有重叠周期的记录



我有一张桌子酒店预订

引用日期ABCDEF2021年8月12日2GHI2021年8月13日3JKL

由于您没有保存结束日期,因此需要将其计算为start_date + interval numberOfDays days以获得独占的结束日期。日期重叠查询看起来像:

SELECT *
FROM t
WHERE @d2 > start_date AND (start_date + interval numberOfDays day) > @d1
-- In order to check [2021-08-11, 2021-08-12]
-- You will set @d1 and @d2 to 2021-08-11 and 2021-08-13

为了完整起见,这里是包含结束日期的版本:

SELECT *
FROM t
WHERE @d2 >= start_date AND (start_date + interval numberOfDays - 1 day) >= @d1
-- In order to check [2021-08-11, 2021-08-12]
-- You will set @d1 and @d2 to 2021-08-11 and 2021-08-12

最新更新