0

我目前正在制作酒店预订系统,想知道如何查询 tbl_prices 表中的价格列。

这是我的桌子的样子:

id          date_from          date_to         price          is_default
1           00-00-0000         00-00-0000      $95               1
2           05-25-2012         05-29-2012      $100              0
3           06-20-2012         06-24-2012      $90               0

第一行是默认价格。因此,如果有人预订不在第 2 行和第 3 行的日期,则价格为 95 美元。但如果预订日期在第 2 排和第 3 排,那么价格应该是 100 美元或 90 美元。

例如,如果客人想在 2012 年 5 月 24 日至 2012 年 5 月 26 日期间入住,则价格应为

第一天 = 95 美元 第 2 天 = 100 美元

5-26-2012 将不收费,因为它是结帐日期。

4

2 回答 2

0

如果您每天查询一次,则只有一个默认值,并且价格没有重叠时段:

SELECT price FROM tbl_prices
WHERE ($input_date >= date_from AND $input_date <= date_to) 
      OR is_default = 1
ORDER BY is_default
LIMIT 1
于 2012-04-19T01:23:26.877 回答
0

一个非常直接的解决方案可以是:

$from = 5-24-2012$till = 5-26-2012

select price, date_to from my_table where date_from <= $from 
ORDER BY date_from ASC LIMIT 1

然后可能您可以在 date_to (由此查询接收)和 $till 之间进行检查。

if($till <= $date_to) {
    // total cost is price * no. of days of stay
}
else {
   // repeat above query, this time with date_to parameter in where clause
   // total cost is price1 * no. of days in 1st range + .....
}
于 2012-04-19T01:21:36.493 回答