如何计算5天(mysql)酒店预订系统的总价

时间:2013-01-06 21:41:03

标签: php mysql sql phpmyadmin

我有这张桌子用于季节(高,低)季节的房间价格

如何在不同的日子和不同的季节获得5天的总价格。

看看这张表

enter image description here

这是我的命令(mysql) 我想从 04-07-2013 11-07-2013

获得10晚的总价
select sum(
case when dayname('2013-01-07') = 'Monday' then coalesce(prices.room_price , def.room_price) 
 when dayname('2013-01-07') = 'Sunday' then coalesce(prices.room_price , def.room_price) 

)as TotalPrice 
from (select strtodate('2013-01-07' , '%Y-%m-%d') as thedate 
select strtodate('2013-01-08' , '%Y-%m-%d') as thedate ) dates left outer join ts_room_prices prices
on dates.thedate between prices.season_start and prices.season_end cross join ts_room prices def on def.season_name = 'default'


除了

我写了这个命令,但仍然没有工作

select sum(coalesce(prices.room_price , def.room_price) ) as TotalPrice
from (select strtodate('2013-01-07' , '%Y-%m-%d') as thedate union all
      select strtodate('2013-01-08' , '%Y-%m-%d') as thedate
     ) dates left outer join
     ts_room_prices prices
     on dates.thedate between prices.season_start and prices.season_end and
        dayname(dates.thedate) = prices.dayofweek join ts_room_prices def
    on def.season_name = 'default' and
       def.hotel = 3233 and
       def.dayofweek = dayname(dates.thedate)

错误:#1305 - 功能saudihot_saudihotels.strtodate不存在

1 个答案:

答案 0 :(得分:1)

以下是查询:

select sum(coalesce(prices.room_price , def.room_price) ) as TotalPrice
from (select strtodate('2013-01-07' , '%Y-%m-%d') as thedate union all
      select strtodate('2013-01-08' , '%Y-%m-%d') as thedate
     ) dates left outer join
     ts_room_prices prices
     on dates.thedate between prices.season_start and prices.season_end and
        dayname(dates.thedate) = prices.dayofweek join ts_room prices def
    on def.season_name = 'default' and
       def.hotel = <whatever the hotel is> and
       def.dayofweek = dayname(dates.thedate)

请注意,sum()表达式更简单,并且星期几现在在连接中。我还添加了获取默认值的酒店条件 - 这也应该在其他查询中。

请记住,您必须将所有日期放在初始子查询中,并与union all结合使用。