以下表格是:
CUSTOMERS (CustomerID, firstname, surname etc)
ROOMS (RoomID, RoomType, Description)
BOOKING (BookingID, CustomerID, Arrival Date, Departure Date, number of people, total cost)
ROOMS_BOOKED (BookingID, RoomID)
用户输入两个日期到达和离开。然后检查房间的可用性。我已经预订了一个房间,我已经自己插入了数据库。因此,BOOKING表现在包括BookingID = 1,CustomerID = 1,到达日期= 24/03/2015,出发日期= 26/03/2015,人数= 1,总成本= 40.00。然后在ROOMS_BOOKED表中我有BookingID = 1和RoomID = 2这是一个标准房间。
我正在使用这个sql代码 -
SELECT RoomType FROM ROOMS WHERE RoomID NOT IN (SELECT RoomID FROM ROOMS_BOOKED)
此查询是返回尚未预订的房间,但不是我正在寻找的房间。例如,用户选择在数据库中预订的相同日期。因此,我希望能够在下一页显示所有房间类型,并显示在这些选定日期预订的特定房型的x或类似内容,类似于www.galgorm.com。
我需要这方面的帮助,每次我发布一个关于这个的问题时,我都得不到答案或某种形式的帮助。到目前为止,我已经使用了这个设计的酒店系统,我现在希望能够完成它。
请帮助我,我确定你有时会遇到需要帮助的情况。
答案 0 :(得分:8)
您有以下情况
The user's selected period:
--------[---------]-------
Booking no1
[-----]-------------------
Booking no2
--------------------[----]
Booking no3
-----[----]---------------
Booking no4
-----------[---]----------
Booking no5
------[-------]-----------
Booking no6
--------------[--------]--
Booking no7
-----[----------------]---
您必须找到跨越的时段。 案例1和2显然是免费的。 案例3,5,6很容易捕获,因为您可以搜索预订的开始日期或预订的结束日期是否在用户的选择范围内。 如果用户的选择日期是在预订之间,则需要查找案例4和7。
以下是免费房间:
DECLARE @ArrivalDate AS DATETIME
DECLARE @DepartureDate AS DATETIME
SELECT RoomType
FROM ROOMS
WHERE RoomID NOT IN
(
SELECT RoomID
FROM BOOKING B
JOIN ROOMS_BOOKED RB
ON B.BookingID = RB.BookingID
WHERE (ArrivalDate <= @ArrivalDate AND DepartureDate >= @ArrivalDate) -- cases 3,5,7
OR (ArrivalDate < @DepartureDate AND DepartureDate >= @DepartureDate ) --cases 6,6
OR (@ArrivalDate <= ArrivalDate AND @DepartureDate >= ArrivalDate) --case 4
)
答案 1 :(得分:3)
此查询列表所有房间和每个房间显示是否在[到达,离开]日期内可用
SELECT
RoomType,
case when NOT EXISTS (SELECT RoomID
FROM ROOMS_BOOKED rb
JOIN BOOKING b on b.BookingID = rb.BookingID
WHERE rb.RoomID = r.Id
and ArrivalDate < 'param Departure Date here'
and DepartureDate > 'param Arrival Date here')
then 1 else 0 end IsAvailable
FROM ROOMS r
答案 2 :(得分:0)
这是一个示例查询,我用它来检查不可用的房间,查询将返回不可用的房间
SELECT roomno FROM tbl_ReservedRooms WHERE ((fromdate BETWEEN @checkindate AND @checkoutdate )
OR (todate BETWEEN @checkindate AND @checkoutdate))
AND status = 'Active'
答案 3 :(得分:-1)
SELECT * FROM room rm WHERE rm.Room_id NOT IN(选择DISTINCT Room_id FROM预订bk WHERE(bk.arrive_date BETWEEN&#39; $ from_date&#39; AND&#39; $ to_date&#39;)或(bk .departure_date BETWEEN&#39; $ from_date&#39; AND&#39; $ to_date&#39;)或(bk.arrive_date&lt;&#39; $ from_date&#39; AND bk.departure_date&gt;&#39; $ to_date&# 39))