我有一張桌子hotel_booking
| 參考 | 開始日期 | 天數 |
|---|---|---|
| 美國廣播公司 | 11-08-2021 | 2 |
| 防御工事 | 12-08-2021 | 2 |
| 全球健康指數 | 13-08-2021 | 3 |
| JKL | 10-08-2021 | 2 |
所以我想要的第一項任務是獲取所有在 x 和 y 日期之間具有 startDate 的預訂,這是直接查詢
select reference
from hotel_booking
where DATE(start_date) BETWEEN '2021-08-11' AND '2021-08-12'
這將回傳 ABC 和 DEF,這是完美的。
現在我想根據 numberOfdays 檢查所有具有重疊時段的預訂
因此,例如,根據 numberOfDays 獲取 startDate 和 endDate 提到的時間段內的所有預訂
輸入:例如,如果 startDate 是 11-08-2021 并且 endDate 是 12-08-2021 那么
預期輸出:回傳的預訂是 JKL、ABC 和 DEF,因為 JKL 有 2 天的預訂,這將與 startDate 11-08-2021 重疊
任何指標將不勝感激
uj5u.com熱心網友回復:
由于您沒有保存結束日期,因此您需要計算它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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353213.html
