0

我有这张表是不同季节(旺季、淡季)房间的价格

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

看看这张表

在此处输入图像描述

这是我的命令(mysql)我想获得从2013年 4 月 7 日 到2013年 7 月 11 日 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 不存在

4

1 回答 1

2

这是查询:

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.

于 2013-01-06T22:06:26.597 回答