[英]MySQL query question: Reservation Like System
Have rooms that may or may not have reservations. 房间可能有或没有保留。 Reservations are from(date) to till(date). 保留时间为(日期)至直到(日期)。 Person searches for rooms by date: from_field and till_field. 人员按日期搜索房间:from_field和till_field。 Trying to find if a room is available. 尝试查找是否有房间。
SELECT rooms.* FROM rooms
LEFT JOIN reservations ON reservations.room_id = rooms.id
WHERE ( reservations.from > till_field OR reservations.till < from_field )
PROBLEM: If a single reservation succeeds it seems that the query has the room as available, even though another reservation occupies the spot. 问题:如果单个预订成功,则查询似乎有可用的空间,即使另一个预订占据了该位置。
How to retrieve rooms with no resevations at all: 如何检索完全没有保留的房间:
In case there is no reservations, there will be no reservations
row returned by the query => you have to check for that also ( reservations.room_id IS NULL
): 如果没有保留,则查询=>返回的没有reservations
行,您还必须检查该行( reservations.room_id IS NULL
):
SELECT rooms.* FROM rooms
LEFT JOIN reservations ON reservations.room_id = rooms.id
WHERE reservations.room_id IS NULL -- if there is no reservations
OR reservations.from > till_field
OR reservations.till < from_field
But to actually get what ou want, you have to check for rooms that don't have any reservation with: 但是要真正获得想要的东西,您必须检查没有预订的房间:
fromDate or tillDate between from_field and till_field
OR
fromDate < from_field and tillDate > till_field
SELECT rooms.*
FROM rooms
WHERE NOT EXISTS (SELECT NULL
FROM reservations
WHERE reservations.room_id = rooms.id
AND ((
reservations.from BETWEEN from_field AND till_field
OR
reservations.till BETWEEN from_field AND till_field
)
OR
(
reservations.from < from_field
AND reservations.till > till_field
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.