我正在研究一个酒店房价系统,而且我正在展示一些我不想要的记录。这是我的表
room id room_name rate_starts rate_ends single_room double_room
1 standard 2014-01-01 2014-02-28 150 200
1 standard 2014-03-01 2014-03-05 200 250
1 standard 2014-03-06 2014-03-31 300 350
2 Garden Villa 2014-01-01 2014-02-28 300 400
这是我的MySQL查询
SELECT room_id, room_name,
SUM(`single_room` * DateDiff(
Least(rate_ends + INTERVAL 1 DAY, '$ends'),
Greatest(rate_starts, '$starts')
)) AS Total
FROM
room_rate
WHERE
hotel_id = '$hotel_id' AND
rate_ends >= '$starts' AND
rate_starts <= '$ends'
GROUP BY room_id
这是我的搜索参数($starts)'2014-02-27'
到($ends)'2014-03-02'
这是我的结果
Standard room 500
Garden Villa 600
我只搜索单人房价。
其实我不想展示花园别墅因为房价不完整。该房间的日期范围不在2014-03-01至2014-03-05之间。但是它仍然显示第一个日期范围结果。
我希望你能理解我的问题。提前致谢
答案 0 :(得分:0)
如果您希望房价在您的起始搜索间隔中,请使用
WHERE
hotel_id = '$hotel_id' AND
rate_starts>= '$starts' AND -- room rate starts in the search interval
rate_ends<= '$ends' --room rate ends in the search interval
我们需要另一种检查可用性的方法 表:
room_id, date, running_availability:
select room_id, date,
(select sum(availability from (select room_id, rate_starts as date, 1 as availability from table
union all
select room_id, rate_ends, -1 as availability from table) as av2 where av1.date <= av2.date) as running_availability
from
(select room_id, rate_starts as date, 1 as availability from table
union all
select room_id, rate_ends, -1 as availability from table) as av2
现在您可以检查房间是否在您的时间间隔内不可用(running_avaliability!= 0以及开始和结束之间的日期)
如果您想一次性检查价格和可用性,您需要加入这两者。为了便于阅读,让我们将此表称为“可用”
SELECT room_id, room_name,
SUM(`single_room` * DateDiff(
Least(rate_ends + INTERVAL 1 DAY, '$ends'),
Greatest(rate_starts, '$starts')
)) AS Total
FROM
room_rate
left join
(select * from available where running_availability!=1) av
on av.date between '$starts' and '$ends'
WHERE
hotel_id = '$hotel_id' AND
rate_ends >= '$starts' AND
rate_starts <= '$ends'
and av.date is null
GROUP BY room_id