[英]Using MySQL query in selecting room availability
I had this database structure in a hotel reservation in terms of room availability 在房间预订方面,我在酒店预订中有此数据库结构
below is my table of room
下面是我的room
桌子
room_num roomtype
101 Single
102 Single
103 Single
111 Deluxe
112 Deluxe
113 Deluxe
114 Deluxe
115 Deluxe
116 Deluxe
121 Superior
122 Superior
and the table below is room_booked
which is records booked room numbers 下表是room_booked
,它记录了预订的房间号
dor
is checkin date and dco
is date checkout dor
是签入日期, dco
是签出日期
room_num dor dco
111 2014-06-01 2014-06-06
112 2014-06-01 2014-06-06
113 2014-06-01 2014-06-06
114 2014-06-01 2014-06-06
115 2014-06-01 2014-06-06
116 2014-06-01 2014-06-06
112 2014-05-18 2014-05-21
113 2014-08-01 2014-08-04
In this case, all of Deluxe room are booked from 2014-06-01
to 2014-06-06
. 在这种情况下,所有豪华客房都从2014-06-01
到2014-06-06
预订。 Then I selected Deluxe room available from 2014-06-01 to 2014-06-06 by using this MySQL code below 然后,我通过使用以下MySQL代码选择了2014年6月1日至2014年6月6日可用的豪华间
SELECT
room.room_num
FROM
room
LEFT JOIN
room_booked ON room_booked.room_num = room.room_num
WHERE
(
(
dor IS NULL AND dco IS NULL
)
OR (
-- wished booking date is before DOR
'2014-06-06' < dor
-- OR wished booking date is after DCO
OR '2014-06-01' >= dco
)
)
-- room type
AND roomtype = 'Deluxe'
However it shows Deluxe room no. 但是显示的是豪华房号。 112 and 113 which is booked before and after selected date range above instead of 0 or fully booked. 在上面所选日期范围之前和之后预订的112和113,而不是0或已满。 Is there any solution to fix this code? 有解决此代码的解决方案吗?
I think you have to check both dates for check in and check out: 我认为您必须同时检查两个日期才能入住和退房:
SELECT
room.room_num
FROM
room
WHERE
roomtype = 'Deluxe' AND
room.room_num not in
(
SELECT
room_booked.room_num
FROM
room_booked
WHERE
(room_booked.dor<='2014-06-1' and room_booked.dco>='2014-06-1')
OR
(room_booked.dor<'2014-06-06' and room_booked.dco>='2014-06-06')
OR
(room_booked.dor>='2014-06-01' and room_booked.dco<'2014-06-06')
)
re-Edited answer and created a sqlfiddle for testing 重新编辑答案并创建一个sqlfiddle进行测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.