2014-03-05 232 views
0
SELECT DISTINCT 
    Room.RoomID, 
    Room.RoomNumber, 
    Room.PhoneExtension, 
    Room.SpecialNotes , 
    RoomType.Description 

FROM   
    Room 

INNER JOIN 
    RoomType 
ON 
    Room.TypeID = RoomType.TypeID 

LEFT JOIN 
    Reservation 
ON 
    Room.RoomID = Reservation.RoomID 

WHERE 
    Room.DeleteFlag=0 AND 
    (Reservation.ResStatus <> 'Occupied' OR Reservation.ResStatus = NULL) AND 
    (Reservation.ResStatus <> 'Reserved' OR Reservation.ResStatus = NULL) AND 
    (Reservation.ResStatus <> 'Confirmed' OR Reservation.ResStatus = NULL) 

ORDER BY 
    RoomNumber 

你好,我有一個刪除房間表格,應該標記一個房間在數據庫中被刪除。房間ID也會在預訂時發佈到預訂表中。我試圖讓所有沒有出現在預訂中的房間,或者如果他們在那張桌子上,那麼他們不應被保留,佔用或確認。過濾SQL查詢

無論預訂狀態如何,以及沒有沒有預定狀態的房間,查詢都會讓我回到所有預訂時顯示的房間。

任何提示將不勝感激。

回答

1

也許我missunderstand你的問題,但試試這個:

SELECT DISTINCT 
    Room.RoomID, 
    Room.RoomNumber, 
    Room.PhoneExtension, 
    Room.SpecialNotes , 
    RoomType.Description 
FROM   Room 
    INNER JOIN RoomType ON Room.TypeID = RoomType.TypeID 
    /*LEFT JOIN Reservation ON Room.RoomID = Reservation.RoomID*/ 
WHERE Room.DeleteFlag=0 And Room.RoomID not in (select RoomID from Reservation where 
    (Reservation.ResStatus <> 'Occupied' OR Reservation.ResStatus = NULL) 
    AND (Reservation.ResStatus <> 'Reserved' OR Reservation.ResStatus = NULL) 
    AND (Reservation.ResStatus <> 'Confirmed' OR Reservation.ResStatus = NULL)) 
ORDER BY RoomNumber 
+1

因爲你在倒車的第二選擇查詢,我改變了<>爲=,不包括空值,並加入他們或代替的AND。工作的一種享受,感謝提示 – PrOjEkTeD