使用这些表阻止房间

时间:2016-02-10 14:38:55

标签: php mysql sql date mysqli

我有以下数据库表

预订tbl

.---------------+-----------------+------------------+---------------.
| reservationID |    guestID      |     check_in     |   check_out   |
+---------------+-----------------+------------------+---------------+
|       1       |       1         |     2016-02-13   |  2016-02-14   |
|       2       |       1         |     2016-02-16   |  2016-02-19   |
.---------------+-----------------+------------------+---------------.

roomreservation tbl

.---------------+-----------------+------------------+---------------+--------------.
| roomReserveID | reservationID   |  date_scheduled  | roomDetailsNo |   roomNo     |
+---------------+-----------------+------------------+---------------+--------------+
|       1       |       1         |     2016-02-13   |      2        |      101     |
|       2       |       1         |     2016-02-13   |      2        |      101     |
|       3       |       4         |     2016-02-16   |      6        |      116     |
|       4       |       4         |     2016-02-17   |      6        |      116     |
|       5       |       4         |     2016-02-18   |      6        |      116     |
|       6       |       4         |     2016-02-19   |      6        |      116     |
.---------------+-----------------+------------------+---------------+--------------.

rooms tbl

.---------+-----------------+-----------.
| RoomID  | RoomDetailsNo   |room_status| 
+---------+-----------------+-----------+
|   101   |         1       | reserved  |
|   102   |         3       | available |
|   103   |         3       | available |
|   104   |         2       | reserved  |
|   105   |         2       | available |
|   116   |         6       | reserved  |
.---------+-----------------+-----------.

roomdetails tbl

.----------------+----------------+---------.
| RoomDetailsNo  |   room_type    |room_rate|
+----------------+----------------+---------+
|        1       | Standard Room  |  3000   |
|        2       | Deluxe Room    |  3500   |
|        3       | Family Room    |  4000   |
|        4       | Standard Quad  |  4000   |
|        5       | Deluxe Quad    |  4000   |
|        6       | Dormitory Room |  4000   |
.----------------+----------------+---------.

及以下我目前的查询是用来阻止2016-02-13至2016-02-14的房间101以及2016-02-16至2016-02-19的116房间。

$roomBlock = mysqli_query($conn, "
SELECT COUNT(r.roomDetailsNo) AS available_rooms, rd.roomDetailsNo, rd.room_type, rd.room_rate,

    r.room_status, rd.room_pax, rd.max_cap, rd.inclusive, rd.description, rd.image_name
    FROM rooms AS r
    LEFT JOIN roomdetails AS rd
    ON r.roomDetailsNo = rd.roomDetailsNo
    WHERE r.roomNo  = (SELECT rr.roomNo
                        FROM roomreservation AS rr
                        WHERE rr.date_scheduled NOT BETWEEN '$newCheckIn' AND '$newCheckOut')
    GROUP BY room_type;
") OR die("Error in roomBlocking:".mysqli_error($conn));

变量$ newCheckIn和$ newCheckOut来自用户,这是他们所需的登记入住和退房日期。如何根据房间保存显示某些日期保留的房间?

当我尝试此查询时,结果为: roomBlocking中的错误:子查询返回多于1行

1 个答案:

答案 0 :(得分:2)

您的子查询返回的结果超过1,这是预期的,因此您应该使用r.roomNo in的地方,其中包括r.roomNo =