简体   繁体   English

酒店预订系统SQL:识别日期范围内的任何可用房间

[英]hotel reservation system SQL: identify any room available in date range

(In case this seems familiar: I asked a different question similar to this one , but I just got a note that the site design has changed and now the owners do want a way to search for any room that is available between a range of dates. So this is a new question...) (如果这似乎熟悉:我问一个类似这样的一个不同的问题 ,但我刚刚得到了一份说明,网站的设计已经改变,现在的业主想办法寻找任何房间是一个日期范围之间的可用那么这是一个新问题...)

I'm working on a hotel reservation system and I need to find which rooms are available between a range of dates. 我正在建立一个酒店预订系统,我需要找到一系列日期之间的房间。 The existing 'availability' table schema is simple, the columns are: 现有的“可用性”表模式很简单,列是:

room_id
date_occupied - a single day that is occupied (like '2011-01-01')

So, if, for example, room #6 is occupied from January 1 to January 5, five rows are added to the availability table, one for each day that room is occupied. 因此,例如,如果从1月1日到1月5日占用#6房间,则可用表中会添加五行,每天占用一个房间。

I'm trying to figure out the query to find what rooms are available between a start and end date (while also filtering on some other parameters like non-smoking, bed size, etc.)... in pseudo-SQL it would be sort of like: 我正在试图找出查询,以查找开始和结束日期之间可用的房间(同时还过滤其他一些参数,如禁烟,床大小等)...在伪SQL中它将是有点像:

 SELECT (a list of room IDs) FROM rooms, availability WHERE rooms.nonsmoking = 1 AND rooms.singlebed = 1 AND nothing between start_date and end_date is in availability.date_occupied 

As with my other question, I'm a bit stuck trying to determine the exact query and how to join the tables it in the most efficient way. 和我的另一个问题一样,我有点卡住试图确定确切的查询以及如何以最有效的方式加入表格。 Thanks for the help. 谢谢您的帮助。

If I understood your db structure properly, you need to find a row in rooms with no corresponding rows in availability. 如果我正确理解了您的数据库结构,您需要在房间中找到一行,并且在可用性中没有相应的行。

SELECT r.* 
FROM rooms r
  LEFT JOIN availability a ON (r.id = a.room_id 
 AND a.date_occupied BETWEEN :start_date AND :end_date)
WHERE a.id IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM