1

I'm developing a Rails 3 application (with MySQL as RDMS) to manage availability/occupancy for some Rooms (it's an hotel-like app) In my case, the problem is I've to keep up in the system 2 different requirements because one room can be rented for:

  1. Some months (for example 6 monts)
  2. Some days/weeks/weekend

Clearly the app has to show for each room in a given date, if it is available or not, and which user is renting; and on the other side, one user can search one room for a given date range. The date range can be some fixed date or some months range (from September to March for instance) and I know this is something not related with the DataBase deisgn but I've to keep in mind.

To get this system working in a efficient way, I thought to create a table YEAR composite by a reference to the Room and an array (representing 1 year) of 365 elements when each element could be 0 or 1, when 0 means available and 1 means rented.

Adopting this solution I'll have to face with some problems:

  1. Return very quick results when an user is searching for a room during some months.
  2. Managing 'cross-years'ranges (for instance, from 20th of December of 2012 to 13rd of January of 2013)
  3. Leap years

and the last thing, I'm tying to avoid SQL procedure or mid-night processes (if it's possible)

I'm sure there are better ways to design this problem in SQL... Could some of you help me? or giving me some hint?

4

1 回答 1

0

保留一份有开始日期和结束日期的房间预订清单。不要尝试对可用性空档进行建模或将您的预订数据库视为电子表格。这只会让你变得毫无意义的复杂性。日期范围很容易使用。

最重要的是要知道如何检测查询中的重叠日期范围。这是测试房间是否已预订或是否免费的基础。假设您有一个 RESERVATION 表,并且您想要查找与给定日期范围重叠的预订:@FromDate 和 @ToDate。您用于查找重叠预订的 WHERE 子句如下所示:

WHERE RESERVATION.start_date < @ToDate 
  AND RESERVATION.end_date > @FromDate

可用房间不会有冲突(即 WHERE NOT IN...),不可用房间会有冲突。

于 2012-04-23T12:32:01.983 回答