PHP MySQL查询选择酒店预订系统中的可用房间

时间:2014-05-23 16:45:55

标签: php mysql sql

目前我正在为酒店预订系统做项目。这是为用户提供自助登记系统,它根据所选的roomtype随机生成房间号。这听起来我的项目对我来说很安静,但我的主管给了我这样做的想法。

到目前为止,我已完成自助登记系统并随机生成房号

现在我对选择可用房间感到困惑

这是room提供的表格

dor是预订日期或签入日期

dco是结帐日期

room_num     roomtype     dor            dco
  101         Single    0000-00-00    0000-00-00
  102         Single    2014-05-29    2014-05-31
  103         Single    0000-00-00    0000-00-00
  111         Deluxe    0000-00-00    0000-00-00
  112         Deluxe    0000-00-00    0000-00-00
  113         Deluxe    2000-00-00    0000-00-00
  114         Deluxe    2014-06-01    2014-06-06
  115         Deluxe    0000-00-00    0000-00-00
  116         Deluxe    2014-06-08    2014-06-11
  121         Superior  0000-00-00    0000-00-00
  122         Superior  0000-00-00    0000-00-00

0000-00-00表示系统尚未选择的房间号。因为系统随机选择了room_num

以下是表格room_booked。以下所有数据均来自rooms

的SQL更新触发器
room_num     roomtype     dor           dco
  102         Single    2014-05-29    2014-05-31
  114         Deluxe    2014-06-01    2014-06-06
  116         Deluxe    2014-06-08    2014-06-11

现在根据所选roomroomtype表中选择可用房间号的SQL代码是什么。哪个不在room_booked上提到的登记日期和结帐日期之间?

提前致谢

5 个答案:

答案 0 :(得分:2)

正如我在comment中已经说过的,我更喜欢另一种数据库结构。所以我先创建了表roomroom_booked

-- DROP TABLE IF EXISTS room_booked;
-- DROP TABLE IF EXISTS room;

CREATE TABLE room (
    room_num INT NOT NULL,
    roomtype ENUM('Single', 'Deluxe', 'Superior') NOT NULL,
    PRIMARY KEY (room_num)
) ENGINE=InnoDB;
CREATE TABLE room_booked(
    id INT NOT NULL,
    room_num INT NOT NULL,
    dor DATE NOT NULL,
    dco DATE NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (room_num) REFERENCES room(room_num)
) ENGINE=InnoDB;

并用原始数据填充

INSERT INTO room (room_num, roomtype) VALUES (101, 'Single');
INSERT INTO room (room_num, roomtype) VALUES (102, 'Single');
INSERT INTO room (room_num, roomtype) VALUES (103, 'Single');
INSERT INTO room (room_num, roomtype) VALUES (111, 'Deluxe');
INSERT INTO room (room_num, roomtype) VALUES (112, 'Deluxe');
INSERT INTO room (room_num, roomtype) VALUES (113, 'Deluxe');
INSERT INTO room (room_num, roomtype) VALUES (114, 'Deluxe');
INSERT INTO room (room_num, roomtype) VALUES (115, 'Deluxe');
INSERT INTO room (room_num, roomtype) VALUES (116, 'Deluxe');
INSERT INTO room (room_num, roomtype) VALUES (121, 'Superior');
INSERT INTO room (room_num, roomtype) VALUES (122, 'Superior');

INSERT INTO room_booked (id, room_num, dor, dco) VALUES (1, 102, '2014-05-29', '2014-05-31');
INSERT INTO room_booked (id, room_num, dor, dco) VALUES (2, 114, '2014-06-01', '2014-06-06');
INSERT INTO room_booked (id, room_num, dor, dco) VALUES (3, 116, '2014-06-08', '2014-06-11');

现在是SELECT语句。在此示例中,用户希望在Deluxe预订2014-06-01个房间。

SELECT
    room_num, roomtype
FROM
    room
WHERE
    room_num NOT IN (
        SELECT
            room.room_num
        FROM
            room
        LEFT OUTER JOIN
            room_booked ON room_booked.room_num = room.room_num
        WHERE
            -- room type
            roomtype != 'Deluxe'
            OR (
                -- wished booking date is after or at the DOR date
                '2014-06-01' >= dor
                -- OR wished booking date is before the DCO date
                AND '2014-06-01' <  dco
            )
    )
ORDER BY
    RAND()
LIMIT 0, 1
;

如果您只在ORDER BY之前使用该部分,则会在Deluxe处获得2014-06-01个可用房间的列表。

答案 1 :(得分:0)

听起来你想要这样的东西:

SELECT TOP 1 ROOM_NUM
FROM TABLE_NAME
WHERE roomtype=varRoomType AND
      dor=0000-00-00

现在我认为TOP 1是一个mysql的东西如果你使用oracle我认为你必须在where子句中添加rownumber = 1。但这应该是第一个找到与某种房型相匹配的开放式房间。

答案 2 :(得分:0)

你可以尝试:

SELECT room_num 
from room 
where roomtype = $roomtype 
and $room_booked 
not between dor and dco

那应该抓住这两个日期之间当前没有预订的房间。

答案 3 :(得分:0)

我从您的问题陈述中了解到,您需要SQL语句,它将提供所有当前可用的预订空间。

这可能有两个解决方案

解决方案1 ​​

SELECT
    room_num
FROM
    room
WHERE
    room_num NOT IN(SELECT room_num FROM room_booked)
    AND roomtype = {ROOMTYPE}
ORDER BY RAND() LIMIT 1

解决方案2

SELECT
    room_num
FROM
    room
WHERE
    dor != '0000-00-00' AND dco != '0000-00-00'
    AND roomtype = {ROOMTYPE}
ORDER BY RAND() LIMIT 1

答案 4 :(得分:0)

你必须随机选择房间,你将在mysql中给出房间类型,你可以使用这个查询

SELECT * FROM room where roomtype='your_given_room_type' and dor='0000-00-00' and dco='0000-00-00' ORDER BY RAND() LIMIT 1

所以它将从可用房间中选择一个随机房间