MySQL检查日期范围是否与其他日期范围重叠?

时间:2017-05-24 09:10:56

标签: php mysql date-range

我正在使用Codeigniter开发汽车租赁系统,其中车辆可以有多个不可用的日期范围。我已经为不可用的日期范围创建了一个单独的表格,现在我希望用户输入一个日期范围并选择那些与这些汽车的不可用日期范围不重叠的汽车。以下是我的代码。

    $d1 = date('Y-m-d', strtotime($pickup));
    $d2 = date('Y-m-d', strtotime($dropoff));
//   $this->db->where($d1." NOT BETWEEN date_ranges.start_date AND date_ranges.end_date AND " . $d2.' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date');
    $this->db->where("'$d1' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date AND '$d2' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date OR ('$d1' < date_ranges.start_date AND '$d2' > date_ranges.end_date) ");
//   $this->db->where(" '$d1' <= date_ranges.end_date AND date_ranges.start_date <= '$d2'");

    $query = ->join('date_ranges', 'vehicles.id = date_ranges.vehicle_id', 'left')
    ->group_by('vehicles.id')
    ->get('vehicles');

原始查询

 SELECT `vehicles`.`id`, `vehicles`.`year-`, `vehicles`.`model`, 
 `vehicles`.`nightly_rate`, `vehicles`.`class`, 
 `vehicle_pictures`.`picture`, 
 `vehicles`.`people` FROM `vehicles` LEFT JOIN `vehicle_pictures` ON 
  `vehicles`.`id` = `vehicle_pictures`.`vehicle_id` LEFT JOIN `date_ranges` 
  ON 
  `vehicles`.`id` = `date_ranges`.`vehicle_id` 
   WHERE ( `country` LIKE '%%' 
    ESCAPE 
    '!' OR `state` LIKE '%%' ESCAPE '!' OR `city` LIKE '%%' ESCAPE '!' OR 
    `street` 
    LIKE '%%' ESCAPE '!' OR `zip` LIKE '%%' ESCAPE '!' )
   AND '2017-05-23' 
   NOT 
   BETWEEN `date_ranges`.`start_date` AND date_ranges.end_date AND '2017-
   05-24' 
   NOT 
   BETWEEN `date_ranges`.`start_date` AND date_ranges.end_date OR ('2017-
   05-23' < 
   `date_ranges`.`start_date` AND '2017-05-24' > date_ranges.end_date) AND 
   `vehicles`.`people` > '1' GROUP BY `vehicles`.`id`;

2 个答案:

答案 0 :(得分:0)

The basic algorithm for a date range that does NOT overlap is:

NOT(unavailable_date_range_START <= desired_date_range_END) AND NOT(unavailable_date_range_END >= desired_date_range_START)

So translated to your query, it should be something like:

 SELECT `vehicles`.`id`, `vehicles`.`year`, `vehicles`.`model`, `vehicles`.`nightly_rate`,
        `vehicles`.`class`, `vehicle_pictures`.`picture`, `vehicles`.`people` 

 FROM `vehicles`

 LEFT JOIN `vehicle_pictures` ON (`vehicles`.`id` = `vehicle_pictures`.`vehicle_id`)

 WHERE (   `country` LIKE '%%' ESCAPE '!'
        OR `state`   LIKE '%%' ESCAPE '!'
        OR `city`    LIKE '%%' ESCAPE '!'
        OR `street`  LIKE '%%' ESCAPE '!'
        OR `zip`     LIKE '%%' ESCAPE '!')
 AND `vehicles`.`id` NOT IN(SELECT DISTINCT `date_ranges`.`vehicle_id`

                            FROM `date_ranges`

                            WHERE `date_ranges`.`start_date` <= '$d2' AND `date_ranges`.`end_date` >= '$d1')

答案 1 :(得分:0)

最后,以下查询对我有用。

SELECT `vehilces`.`id`, `vehilces`.`year`, `vehilces`.`model`, 
`vehilces`.`nightly_rate`, `vehilces`.`class`, `vehilce_pictures`.`picture`, 
`vehilces`.`people`, `date_ranges`.`start_date`, `date_ranges`.`end_date` FROM 
`vehilces` LEFT JOIN `date_ranges` ON `vehilces`.`id` = 
`date_ranges`.`vehilce_id` LEFT JOIN `vehilce_pictures` ON `vehilces`.`id` = 
`vehilce_pictures`.`vehilce_id` WHERE ( `country` LIKE '%%' ESCAPE '!' OR 
`state` LIKE '%%' ESCAPE '!' OR `city` LIKE '%%' ESCAPE '!' OR `street` LIKE 
'%%' ESCAPE '!' OR `zip` LIKE '%%' ESCAPE '!' ) AND vehilces.id NOT IN(SELECT 
DISTINCT vehilce_id from date_ranges WHERE ('$d1' <= end_date)  and  ('$d2' >= 
start_date) or ('$d1' <= end_date)  and  (start_date <= '$d2')) AND 
`vehilces`.`people` > '1' GROUP BY `vehilces`.`id`