想象一下这个简单的数据库表结构。
ROOM
id | id_HOTEL | id_PRICE
BOOKING
id | id_ROOM | date_from | date_to
我需要从房间选择id_HOTEL = 1和id_PRICE = 1的房间以及BOOKING的相应房间,其中两个给定的日期不是重叠date_from和date_to。
如何编写此SQL查询?
答案 0 :(得分:0)
SELECT *
FROM BOOKING AS b
WHERE b.ID_ROOM IN
( SELECT r.ID
FROM ROOM AS r
WHERE r.ID_HOTEL = 1
AND r.ID_PRICE = 1)
AND b.DATE_FROM NOT BETWEEN :GIVEN_DATE_FROM AND :GIVEN_DATE_TO
AND b.DATE_TO NOT BETWEEN :GIVEN_DATE_FROM AND :GIVEN_DATE_TO