显示两个日期之间的可用房间? SQL

时间:2014-06-18 12:04:34

标签: java sql querying

我希望有人能在这里给我一些帮助......

我正在为酒店编制一个小预订程序,在那里我可以看到两个给定日期之间的可用房间列表。我遇到了一个问题,因为如果我在预订到达日期之后搜索到达日期,房间仍会显示为可用...

我对数据库的调用如下所示:

String SQLString = "select roomnumber, roomtypeid from rooms "
        + "where roomnumber not in "
        + "(select roomnumber from booking "
        + "where arrival between to_date(?, 'yyyy-mm-dd') "
        + "and to_date(?, 'yyyy-mm-dd')) and roomtypeid = ? order by roomnumber";

我尝试了一些不同的sql字符串组合,但没有任何帮助。任何人都可以给我一个很好的暗示吗?

CREATE TABLE BOOKING(
ARRIVAL             DATE ,
DEPARTURE           DATE,
RESERVATIONSNUMBER  INTEGER NOT NULL PRIMARY KEY,
ROOMNUMBER          INTEGER,
PAID                INTEGER,
CUSTOMERID INTEGER,
NUMBEROFGUESTS INTEGER,
CONSTRAINT BOFK FOREIGN KEY(ROOMNUMBER) REFERENCES ROOMS(ROOMNUMBER),
CONSTRAINT CUIDFK FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER(CUSTOMERID));

3 个答案:

答案 0 :(得分:1)

以下是查找重叠预订的方法。您可能希望将< =和> =更改为<和>,如果当天不被视为问题。

select roomnumber, roomtypeid
from rooms 
where roomnumber not in 
(
  select roomnumber 
  from booking 
  where departure >= to_date(? /* desired arrival date */, 'yyyy-mm-dd')
  and arrival <= to_date(? /* desired departure date */, 'yyyy-mm-dd')
)
and roomtypeid = ? /* desired room type */
order by roomnumber;

答案 1 :(得分:0)

我认为唯一的问题是日期格式将所有日期字段转换为日期并检查相同。

答案 2 :(得分:0)

它看起来像这样 -

SELECT *
FROM rooms r
WHERE NOT EXISTS (SELECT 1
                  FROM booking b
                  WHERE (?arrival_date_parameter BETWEEN arrival AND departure
                     OR arrival BETWEEN ?arrival_date_parameter AND ?departure_date_parameter)
                    AND r.roomnumber = b.roomnumber)
  AND roomtypeid = ?
ORDER BY roomnumber