2012-05-15 112 views
0

我將嘗試開始日期和結束日期,以顯示我的酒店的免費客房LEFT JOIN不工作

enter image description here

當我使用這個代碼只顯示我的房間至極出現在保留意見,但是它不要讓我看到從未預訂過的房間。

SELECT room.RoomID,room.Room_Type_ID,room.Room_number,room.NumberofSpots, 
res.Data_Check_in,res.Data_Check_out FROM  

     dbo.Reservation res JOIN dbo.Room room ON room.RoomID=res.Room_ID 
     WHERE NOT(res.Data_Check_in<[email protected]_StartData AND res.Data_Check_out>[email protected]_EndData) 

當我使用此代碼它的展示我所有的房間甚至那些保留:

SELECT DISTINCT r.* 
FROM dbo.Room r LEFT JOIN dbo.Reservation res ON r.RoomID=res.Room_ID 
AND NOT(res.Data_Check_in<='2012-05-07' AND res.Data_Check_out>='2012-06-13') 
AND res.Cancel=0 

我應該怎麼修改讓所有沒有這些房間留作選定的日期?

+2

這很好。也許如果你問了一個問題,你可能會得到更好的結果。 –

+0

可能有些問題()等首先你需要一個左連接而不是內連接。其次將整個條件包含在()中。 – JonH

+0

如果有人在指定日期退房,其他人是否可以在同一天登記入住? – HABO

回答

2

請原諒在變量名和列名的使用日期,而不是數據。

declare @p_StartDate as Date = '2012-05-07' 
declare @p_EndDate as Date = '2012-06-13' 

declare @Room as Table (Room_ID Int Identity, Room_number VarChar(5)) 
insert into @Room (Room_number) values 
    ('101'), ('102'), ('103'), ('104'), ('105'), 
    ('201'), ('202'), ('203'), ('204'), ('205') 

declare @Reservation as Table (ReservationID Int Identity, Room_ID Int, Date_Check_in Date, Date_Check_out Date, Cancel Bit) 
insert into @Reservation (Room_ID, Date_Check_in, Date_Check_out, Cancel) values 
    (3, '2012-05-01', '2012-05-06', 0), -- Before. 
    (3, '2012-06-14', '2012-07-01', 0), -- After. 
    (4, '2012-05-07', '2012-06-13', 0), -- Matching. 
    (5, '2012-06-01', '2012-06-05', 0), -- Within. 
    (6, '2012-05-01', '2012-06-01', 0), -- Overlapping start. 
    (7, '2012-06-01', '2012-06-15', 0), -- Overlapping end. 
    (8, '2012-06-01', '2012-06-05', 1), -- Within, but cancelled. 
    (9, '2012-06-01', '2012-06-15', 1), -- Overlapping, but cancelled. 
    (10, '2012-01-01', '2012-12-31', 0) -- Containing. 

select room.Room_ID, room.Room_number 
    from @Room as room 
    where not exists (
    select 42 
     from @Reservation 
     where Room_ID = room.Room_ID and Cancel = 0 and 
     -- The date ranges overlap. 
     (((@p_StartDate <= Date_Check_in) and (Date_Check_in <= @p_EndDate) or 
     (@p_StartDate <= Date_Check_out) and (Date_Check_out <= @p_EndDate)) or 
     -- The desired range is contained in the reserved range. 
     ((Date_Check_in <= @p_StartDate) and (@p_EndDate <= Date_Check_out)))) 
+0

+1除了範圍重疊檢測可以更簡單之外,這很棒。請看看[確定是否兩個日期範圍重疊](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap)。 –

+0

@NikolaMarkovinović - 確實如此!我將把它作爲讀者的練習。 – HABO

+0

它工作,非常棒 – jonny

0

如何

SELECT Dr.* FROM dbo.Room r LEFT JOIN dbo.Reservation res 
ON r.RoomID=res.Room_ID 
WHERE res.Room_ID IS NULL 

對於房間永遠保留。

左連接不會與房間保留但不是今天幫助。對於你想要的東西像

SELECT Room_ID FROM Reservation WHERE Data_Check_out<=? OR Data_Check_in>=? 
OR (Data_Check_out<=? AND Data_Check_in<=? AND Cancel=1) 

一些很好的意見。我們知道左邊的加入會給我們「從未用過的房間」。

如果沒有取消字段以下應該工作:

組@checkindate = '2012-05-15';

組@checkoutdate = '2012-05-17';從預訂

選擇ROOM_ID哪裏都不( Data_Check_in和Data_Check_out OR Data_Check_in和Data_Check_out之間@checkoutdate之間@checkindate)

但取消使事情變得更難,因爲我們需要知道,所有的日子希望可用。這種感覺更像是在個別日子裏設定的操作。

+0

第一個很好,但我想按日期選擇房間 – jonny

+0

但是,如果沒有預訂,你不能按日期選擇房間!如果沒有預訂,那麼在您感興趣的那一天就沒有預訂,這樣就可以。其餘的,第二個查詢更合適。您可以將兩者結合起來, – Julian

+0

手頭的任務似乎是指定時間段內沒有保留的識別房間,例如,有人想要下個星期所有的房間,哪些房間可用?令人討厭的是檢測到請求的時間段和未完成預訂之間的重疊日期範圍。 – HABO