两个表的SQL查询酒店房间(类型和可用性)

时间:2014-06-04 14:59:34

标签: sql sql-server

第一张表'预订'包含From_date,To_date和Room_ID。

第二张桌子'房间'包含Room_ID和随机列Room_Floor。

 Booking
 From_date || To_date || Room_ID

 Room
 Room_ID || Room_Floor
  

a)从特定日期到特定日期,在1楼预订的房间会产生什么样的查询。

     

b)什么查询会导致二楼的可用房间   例如,从特定日期到特定日期。

提前谢谢你。非常感谢任何帮助。

使用MS SQL 2008.

2 个答案:

答案 0 :(得分:2)

`SELECT * from Room R
INNER JOIN Booking B on B.Room_ID = R.Room_ID
where Room_Floor = 1
AND From_date BETWEEN GETDATE() AND To_date
`

这将找到1楼的所有房间预订

`SELECT * from Room R
where not exists (select * from bookings where Room_ID = R.RoomID and GETDATE()
Between From_date AND To_date)
and Room_Floor = 2`

这将找到2楼的所有可用房间

我想的那样

答案 1 :(得分:0)

非常感谢user2270653。这就是我使用user2270653指南解决查询的方法。

预订房间1楼:

SELECT [Room_Number], [Room_Floor], [Room_Position], [Room_Class], 
[Room_Beds], [Room_Price] FROM [Room]  INNER JOIN [Booking] 
ON Booking.Room_ID = Room.Room_ID
WHERE [Booking_To]  >= '2014/05/20' 
AND [Booking_From]  <= '2014/07/25'
AND [Room_Floor]='1'

可用房间2楼:

SELECT [Room_ID], [Room_Number], [Room_Floor], [Room_Position], [Room_Class],    
[Room_Beds], [Room_Price] FROM [Room] where not exists (SELECT [Room_ID] 
FROM [Booking] WHERE Booking.Room_ID = Room.Room_ID 
AND [Booking_To]  >= '2014/05/20' 
AND [Booking_From] <= '2014/07/25')
AND [Room_Floor]='2'