【发布时间】:2014-06-14 06:33:15
【问题描述】:
我正在构建一个酒店预订系统,但在尝试进行查询以检索给定范围内的所有可用房间类型时卡住了。
我有两张桌子Rooms 和Reservations。 Rooms 桌子存放着酒店的房间。他们的号码 (id) 和他们的类型 (type)。
Table Reservations 保存客户的预订。预订号 (id)、相关房间号 (room_id) 和日期范围(from 和 to)
我试过这个查询:
SELECT room_id as available_room_number, type
FROM roomstesting
LEFT JOIN reservations ON roomstesting.id = reservations.room_id
WHERE reservations.id
NOT IN (reservations.e_from <='"2014-03-07 19:00:00'
AND reservations.e_to >='2014-03-08 19:00:00')
我试图获取 3 月 7 日至 3 月 8 日范围内的所有可用房间类型。我希望得到 modern room 作为查询的结果。因为modern room id 4 没有与日期范围重叠的预订,并且所有其他 3 个房间都有从 3 月 6 日到 3 月 9 日的预订。但我没有得到我想要的结果。下面是我的数据库的结构(简化)
房间
| id | type |
|||||||||||||||||||||
| 1 | master suite |
| 2 | master suite |
| 3 | modern room |
| 4 | modern room |
预订
| id | room_id | from | to |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1 | 1 | 2014-03-05 08:00:00 | 2014-03-09 08:00:00 |
| 2 | 2 | 2014-03-05 08:00:00 | 2014-03-09 08:00:00 |
| 3 | 3 | 2014-03-05 08:00:00 | 2014-03-09 08:00:00 |
预期结果
| available_room_number | type |
||||||||||||||||||||||||||||||||||||||
| 4 | modern room|
如果有人能告诉我应该如何处理这个问题,那就太完美了。期待您的回复。
【问题讨论】: