如何檢索配有沒有resevations可言:
如果沒有預訂,將有查詢返回的沒有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
)
)
)
是什麼問題? –
LEFT JOIN導致問題,當沒有任何保留時它不會返回房間。 – John