我正在办理酒店预订模块,几天后我就堆叠了。
Tables
+++++++++++++++++++++++++++++++
rooms bookings
======== =============
room_id b_id
avabile_rooms b_room_id
check_in
check_out
b_rooms
==其中== avabile_rooms - 房间的数量 b_rooms =预订了此b_id的房间数
Values needed
++++++++++++++++++++++++++++++++
room_id and number_of_rooms_avabile foreach avabile room in interval A(check_in) - B(check_out)
Current Query
++++++++++++++++++++++++++++++++
SELECT r.* FROM rooms AS r WHERE r.room_id NOT IN
(
SELECT b.b_room_id FROM bookings AS b
WHERE (b.check_out >= ? AND b.check_in <= ?)
OR (b.check_out <= ? AND b.check_in >= ?)
)
现在我在不考虑avabile_rooms / b_rooms的情况下获得了可用的房间
The Unkown Query (I thik It needs to be something like this)
++++++++++++++++++++++++++++++++
SELECT r.* FROM rooms AS r WHERE r.room_id IS IN
(
SELECT b.b_room_id FROM bookings AS b
OR b.b_id IS NULL
OR (b.check_out >= ? AND b.check_in <= ?)
OR (b.check_out <= ? AND b.check_in >= ?)
)
我不知道如何获得room_id / number_of_rooms_avabile P.S。:我做了很深入的搜索,但没有找到考虑到房间数量的解决方案。 感谢。
答案 0 :(得分:1)
我将如何做到这一点:
select rt.room_type_id, available = rt.available_rooms - isnull(sum(b.b_rooms), 0)
from room_types rt
left join bookings b
on rt.room_type_id = b.b_room_type_id
and b.check_in <= @end_date
and b.check_out >= @start_date
group by rt.room_type_id, rt.available_rooms
我按照评论中的说明将您的房间表重命名为room_types,并根据需要重命名列。
这将占用可用房间的数量并减去与所选日期范围有任何重叠的所有预订,我相信这是您想要的。
根据您的业务规则,您可能需要这样做:
where b.check_in < @end_date
and b.check_out > @start_date