[英]MySQL query question: Reservation Like System
房间可能有或没有保留。 保留时间为(日期)至直到(日期)。 人员按日期搜索房间:from_field和till_field。 尝试查找是否有房间。
SELECT rooms.* FROM rooms
LEFT JOIN reservations ON reservations.room_id = rooms.id
WHERE ( reservations.from > till_field OR reservations.till < from_field )
问题:如果单个预订成功,则查询似乎有可用的空间,即使另一个预订占据了该位置。
如何检索完全没有保留的房间:
如果没有保留,则查询=>返回的没有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
但是要真正获得想要的东西,您必须检查没有预订的房间:
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.