如何查询开始日/月和结束日/月之间的价格

时间:2011-10-04 12:43:51

标签: php mysql

当天是4,月份是10。 如何查询月份在start_month和end_month范围之间的价格,它们的日期也在一个范围之间?

start_day,start_month,end_day和end_month可以在数据库中变量。

数据库的屏幕截图: Database http://f.cl.ly/items/0D1g1k3l1Y2h1D413q16/Screen%20Shot%202011-10-04%20at%2014.38.23.png

我当前的查询:

$price = FrontendModel::getDB()->getVar('SELECT p.price FROM appartments_pricing as p WHERE (p.days_start >= ? AND p.months_start >= ?) AND (p.days_end <= ? AND p.months_end <= ?) AND p.appartment_id = ?', array($day,$month, $day, $month, $appartment_id));

2 个答案:

答案 0 :(得分:1)

好的另一个选择:

mysql> select
           ap_id, price,
           date(concat('2000', '-', months_start, '-', days_start)) as start,
           date(concat('2000', '-', months_end, '-', days_end)) as end
           from x
           having date('2000-10-4') > start and date('2000-10-4') < end;

+-------+-------+------------+------------+
| ap_id | price | start      | end        |
+-------+-------+------------+------------+
|    32 |    15 | 2000-01-01 | 2000-12-31 |
+-------+-------+------------+------------+

答案 1 :(得分:0)

如果您希望白天在范围days_start <= day <= days_end,那么您需要切换查询(月份相同):

$price = FrontendModel::getDB()->getVar('SELECT p.price FROM
appartments_pricing as p WHERE (p.days_start <= ? AND p.months_start <= ?) AND
(p.days_end >= ? AND p.months_end >= ?) AND p.appartment_id = ?',
array($day,$month, $day, $month, $appartment_id));