建议的伪代码仅考虑现有预订和新预订之间发生冲突的一种可能性。举例来说,考虑一个有一个预订的房间。
|======================|
Check-in date Check out date
假设我们要创建一个新预订,并且有 4 个潜在的新预订。
|======================| Existing booking
|-----------------------------| New booking (Scenario 1)
|----------| New booking (Scenario 2)
|-------------| New booking (Scenario 3)
|---| New booking (Scenario 4)
其中,只有场景 4 不与现有预订重叠;其他与现有预订冲突。虽然您的伪代码针对场景 1,但它不会检测场景 2 或 3,因此允许重复预订。
实际上,您的伪代码可能看起来像这样。
Let E = booking already on the database
N = new booking,
CID = check-in date,
COD = check-out date
For a new booking N, N conflicts with an existing booking iff there exists a record E where:
(CID of E is between CID of N and COD of N), or
(COD of E is between CID of N and COD of N), or
(CID of N < CID of E and COD of N > COD of E)
在 SQL 中,根据您的架构,查询可能类似于以下内容:
-- assume @new_cid is the new checkin date and @new_cod is the new checkout date
select count(*) from bookings
where
@new_cid between bookings.checkindate and bookings.checkoutdate or
@new_cod between bookings.checkindate and bookings.checkoutdate or
(@new_cid <= bookings.checkindate AND @new_cod > bookings.checkoutdate)