预订数据库中的可用性

时间:2016-05-05 16:26:28

标签: sql database

我有一个酒店预订数据库,其中有桌子:房间客户预订 id ,< em> id_room , id_customer _ arri_date departure_date ,...)。

当我在我的应用程序中选择一个房间时,我需要查看一个日历小部件,如果那天房间很忙,天红色。

我需要一种方法来检索房间,月份,年份组合的忙碌日期列表。

我的想法是使用以下列创建一个新表: date day month year 房间 is_busy 然后查询。

SELECT day FROM new_table WHERE month=m AND year=y AND room=r AND is_busy=1

问题是每次都要更新新表。

有简单的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此脚本。从现在起,它将在接下来的255天内返回每个房间的繁忙日期。

SELECT * INTO reservation
FROM (VALUES (1, 1, '2016-07-03','2016-07-06'),(2, 2, '2016-07-10','2016-07-15')) 
a(CustomerID, RoomID, arrive_date, departure_date);
GO
;WITH Pass0 as (select 1 as C union all select 1),
  Pass1 as (select 1 as C from Pass0 as A, Pass0 as B),
  Pass2 as (select 1 as C from Pass1 as A, Pass1 as B),
  Pass3 as (select 1 as C from Pass2 as A, Pass2 as B),
  FutureDates(FutureDates) as (SELECT DATEADD(DAY,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1, CAST(GETDATE() AS DATE)) FROM Pass3)
SELECT r.RoomID, f.FutureDates as BusyDate
FROM reservation as r
INNER JOIN FutureDates as f
ON f.FutureDates >= r.arrive_date and f.FutureDates < r.departure_date;