2017-05-24 255 views
0

我正在使用Codeigniter的汽車租賃系統,其中車輛可能有多個不可用的日期範圍。我爲不可用的日期範圍創建了單獨的表格,現在我希望用戶輸入日期範圍並選擇所有不與這些車輛的不可用日期範圍重疊的車輛。以下是我的代碼。MySQL檢查日期範圍是否與其他日期範圍重疊?

$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

的[檢查日期範圍重疊在MySQL]可能的複製(https://stackoverflow.com/questions/2545947/check-overlap-of-date-ranges-in-mysql) – JazZ

回答

0

對於不重疊的日期範圍內的基本算法是:

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

所以翻譯爲您所查詢的,應該是這樣的:

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') 
+0

它唯一的工作,如果在表格中有一個不可用的範圍,而在我的情況下,每個車輛可能有多個不可用的日期範圍。 – BTree

+0

如果有多個不可用範圍,你會得到什麼? – silkfire

+0

如果同一輛車有多個不可用的範圍,它甚至會選擇範圍重疊的車輛! – BTree

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`