1
Hotel_id Room_id Room_type 开始日期 结束日期 价格
-------------------------------------------------- --------------
   13 2 标准 2012-08-01 2012-08-15 7000
   13 2 标准 2012-08-16 2012-08-31 7500
   13 2 标准 2012-09-01 2012-09-30 6000
   13 3 豪华 2012-08-01 2012-08-15 9000
   13 3 豪华 2012-08-16 2012-08-31 10000
   13 3 豪华 2012-09-01 2012-09-30 9500

嗨,这是我的表的结构和数据。

我需要为酒店预订创建一个 mysql 查询,该查询将匹配数据库用户输入的数据:

  1. 他们想要入住和退房的日期
  2. 房型

例如:

如果用户根据这些日期(2012 年 8 月 30 日至 2012 年 9 月 4 日)选择带豪华客房的酒店,则总费用将为 8 月 30 日和 31 日的 (10000*2) + 1 日和 2 日的 (9500*3) 9 月 3 日(不包括第 4 个结账日),即总价为 20000+28500=48500

因此查询应根据 Hotel_id、Room_id、Start_date、End_date 和 Price 过滤总价

谢谢

4

3 回答 3

4

使用此解决方案:

SELECT     SUM(
               CASE WHEN a.Start_date = b.min_sd AND a.Start_date <> b.max_sd THEN
                        (DATEDIFF(a.End_date, '2012-08-30')+1) * a.Price
                    WHEN a.Start_date = b.max_sd AND a.Start_date <> b.min_sd THEN
                        DATEDIFF('2012-09-04', a.Start_date) * a.Price
                    WHEN (a.Start_date,a.Start_date) IN ((b.min_sd,b.max_sd)) THEN
                        (DATEDIFF('2012-09-04', '2012-08-30')+1) * a.Price
                    WHEN a.Start_date NOT IN (b.min_sd, b.max_sd)             THEN
                        (DATEDIFF(a.End_date, a.Start_date)+1) * a.Price
               END 
           ) AS totalprice
FROM       rooms a
CROSS JOIN (
           SELECT MIN(Start_date) AS min_sd,
                  MAX(Start_date) AS max_sd
           FROM   rooms
           WHERE  Room_type   = 'luxury'     AND
                  End_date   >= '2012-08-30' AND
                  Start_date <= '2012-09-04'
           ) b
WHERE      a.Room_type   = 'luxury'     AND
           a.End_date   >= '2012-08-30' AND
           a.Start_date <= '2012-09-04'

分别用您输入的开始日期和结束日期替换出现的2012-08-30和。2012-09-04

这将考虑到开始日期和结束日期在同一个月以及跨越多个月。


SQLFiddle 演示

于 2012-08-04T18:34:47.320 回答
2

您可以使用 MySQL 的BETWEEN ... AND ... 运算符来查找所需预订的日期范围(请记住从给定的结帐日期休息一天,就像您说的那样,没有住宿),然后按房间对结果进行分组并取 SUM()价格乘以夜数(可以使用 MySQL 的LEAST()GREATEST()函数计算):

SELECT   Room_id,
         SUM(Price * (1 + DATEDIFF(
              LEAST(End_date,   '2012-09-04' - INTERVAL 1 DAY),
           GREATEST(Start_date, '2012-08-30')
         ))) AS Total
FROM     mytable
WHERE    Room_type = 'luxury' AND (
              '2012-09-04' - INTERVAL 1 DAY
                           BETWEEN Start_date AND End_date
           OR '2012-08-30' BETWEEN Start_date AND End_date
         )
GROUP BY Room_id

sqlfidde上查看。

于 2012-08-04T12:28:39.670 回答
0

试试这个:

set @Hotel_id :=13;
set @Room_id :=3;
set @Start_date :='2012-08-30'  ;
set @End_date :='2012-09-04';

 select sum(b.TotalPrice-b.deductions) as total_cost from
( select a.Price,a.StartDate,a.EndDate,price*(DATEDIFF(a.EndDate,a.StartDate)+1) as TotalPrice  
 ,case when a.EndDate=@End_date then a.Price else 0 end as deductions
 from
(select price,case when @Start_date>=Start_date then  @Start_date else Start_date end as StartDate
,case when @End_date<=End_date then  @End_date else End_date end as EndDate
 from h_booking h1 
where  Hotel_id=@Hotel_id
and Room_id=@Room_id
and (@Start_date between Start_date and End_date or @End_date between Start_date and End_date ))a )b
于 2012-08-04T09:05:53.590 回答