建議的僞代碼僅考慮現有預訂和新預訂之間存在衝突的一種可能性。爲了說明,考慮一個房間有一個預訂。
|======================|
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)
來源
2013-11-24 17:32:37
drf
我會試一試,但你已經完全解釋了我正在嘗試的內容!對不起,我的嘗試很痛苦,所以謝謝你的理解。再次感謝你! – ConnorL