我在酒店预订中根据房间可用性设置了这个数据库结构
下面是我的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
,下表是room_booked
,这是记录预订房间号
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
在这种情况下,所有豪华客房都从2014-06-01
预订到2014-06-06
。然后我通过使用下面的MySQL代码选择了2014-06-01到2014-06-06的豪华房间
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'
然而它显示豪华房号。在上面的选定日期范围之前和之后预订的112和113而不是0或完全预订。有没有解决方法来修复此代码?
答案 0 :(得分:3)
我认为您必须检查两个日期以便办理登机手续并查看:
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')
)
重新编辑了答案并创建了sqlfiddle进行测试