0
SELECT HOTEL.ROOM
FROM HOTEL
  LEFT OUTER JOIN BOOKED
 ON BOOKED.ROOM = HOTEL.ROOM

 WHERE
    TO_DATE('&STARTDATE','DD-MM-YY') BETWEEN BOOKED.ARRIVALDATE 
 AND BOOKED.ARRIVALDATE + &NIGHTS

我需要帮助,我需要搜索已预订的房间并返回可用的房间。基本上,用户在查询中输入一个日期,以及他住的晚数。这是在 BOOKED 表中查找的,如果日期相等,则表示房间已预订,不应从 HOTEL.ROOM 表中选择该房间。编辑:将 RIGHT OUTER JOIN 更改为 LEFT OUTER JOIN,刚刚意识到它正在从错误的表中选择数据

4

1 回答 1

0

您的日期范围内的预订:

SELECT *
FROM   BOOKED
WHERE  TO_DATE('&STARTDATE','DD-MM-YY') BETWEEN ARRIVALDATE AND ARRIVALDATE + &NIGHTS

扩展以查找HOTEL此列表中的房间,即已预订:

SELECT ROOM
FROM   HOTEL
WHERE  EXISTS (
         SELECT *
         FROM   BOOKED
         WHERE  TO_DATE('&STARTDATE','DD-MM-YY') BETWEEN ARRIVALDATE AND ARRIVALDATE + &NIGHTS
         AND    ROOM = HOTEL.ROOM
       )

而那些不在此列表中的即未预订:

SELECT ROOM
FROM   HOTEL
WHERE  NOT EXISTS (
         SELECT *
         FROM   BOOKED
         WHERE  TO_DATE('&STARTDATE','DD-MM-YY') BETWEEN ARRIVALDATE AND ARRIVALDATE + &NIGHTS
         AND    ROOM = HOTEL.ROOM
       )
于 2013-11-05T09:47:50.443 回答