[英]check availability of room in hotel
There are two tables in my database. 我的数据库中有两个表。 First table is room and second table is reservation. 第一张桌子是房间,第二张桌子是预订。 In my room table 在我的房间桌子上
id room_no type rate
1 13 1b 1000
2 14 2b 2000
3 15 3b 3000
4 16 1b 1000
5 17 2b 2000
6 18 3b 3000
In my reservation table 在我的预订表中
id room_no check_in check_out
1 13 23-2-2016 24-2-2016
2 14 24-2-2016 25-2-2016
1 13 25-2-2016 26-2-2016
1 13 27-2-2016 29-2-2016
1 13 1-3-2016 2-3-2016
1 13 7-3-2016 7-3-2016
"SELECT room_no,type,rate
FROM room
WHERE room_no not in
(select IFNULL(GROUP_CONCAT(room_no),0)
FROM reservation
WHERE check_out >= '$check_in' AND check_in <= '$check_out')"
when I select a date 24-2-2016 to 27-2-2016 then it display 当我选择一个日期24-2-2016到27-2-2016时,它将显示
room_no check_in check_out
14 24-2-2016 25-2-2016
15 25-2-2016 26-2-2016
16 27-2-2016 29-2-2016
17 1-3-2016 2-3-2016
18 7-3-2016 7-3-2016
but I want all available rooms. 但我想要所有可用的房间。
To get occupied rooms for the period specified, ie '2016-02-27'-'2016-02-24'
, you can use: 要在指定的时间段内(即'2016-02-27'-'2016-02-24'
房间,您可以使用:
SELECT DISTINCT room_no
FROM reservation
WHERE check_in <= '2016-02-27' AND check_out >= '2016-02-24'
Output: 输出:
room_no
=======
13
14
To get available rooms you can use the previous query like this: 要获得可用的房间,您可以使用上一个查询,如下所示:
SELECT *
FROM room
WHERE room_no NOT IN (
SELECT DISTINCT room_no
FROM reservation
WHERE check_in <= '2016-02-27' AND check_out >= '2016-02-24')
Output: 输出:
id, room_no, type, rate
=======================
3, 15, 3b, 3000
4, 16, 1b, 1000
5, 17, 2b, 2000
6, 18, 3b, 3000
You should use something like this: 您应该使用这样的东西:
"SELECT room_no, type, rate
FROM room
WHERE room_no not in
(select room_no FROM reservation
WHERE ('$check_in' BETWEEN check_in AND check_out) AND ('$check_out' BETWEEN check_in AND check_out))"
I don't know if the query is correct, but you surely have to check for between. 我不知道查询是否正确,但是您一定要检查一下。 You can't just test for check_out > than date, and check_in < date. 您不能只测试check_out>比date和check_in <date。 IF you have multiple reservations this will give you Errors!! 如果您有多个保留,这将给您错误!
Using a LEFT JOIN
should do the trick without using a subquery. 使用LEFT JOIN
应该可以解决问题,而无需使用子查询。
Something like this : 像这样的东西:
SELECT
room.room_no,
room.`type`,
room.rate,
COUNT(reservation.room_no) AS countReservation
FROM
room
LEFT JOIN reservation
ON (room.room_no = reservation.room_no)
AND (check_in <= '2016-02-24' AND check_out >= '2016-01-27')
GROUP BY
room.room_no
HAVING
countReservation = 0
An other advantage in this query is the extra column countReservation
that will even tell you if you have 1 or more reservations for the for the given timeframe for each room. 此查询的另一个优点是额外的列countReservation
,甚至可以告诉您在每个房间的给定时间范围内您是否有1个或多个保留。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.