2016-05-24 46 views
0

Hotel Rooms Reservation ER DiagramCodeIgniter SQL查詢不返回邏輯結果

我正在使用CodeIgniter框架。我想在特定的日期獲得所有可用的房間。算法如下:

1)我將給出一個日期來檢查房間的可用性。

2)系統應看到表「hotelbrancheshasreservations」和基於分支的ID和status = 'active'.

3應選擇預約IDS)然後,所有的活性保留被選擇和被接合與保留表

4)只有那些保留是從提取的「保留」表中提取後的表,它們不在提供的日期保留並且是活動的。這張表中會有數千個保留,但應該跳過。

5)然後積極保留加入「保留房間」表,這將提供房間號。雖然目前保留,但可以爲與當前保留日期沒有衝突的其他日期保留。 6)最後,當前預訂的房間也加入了「酒店房間」表和所有可用的房間返回新的預訂。

7)所以一個房間可以預訂多次,但日期不同。

我無法在CodeIgniter中開發適當的查詢來獲取所有可用房間,如果我提供一些日期,如$startdate = 2016-05-16$enddate = 2016-05-27

數據類型MySQL的 Db的是date

我運行下面的查詢中

function joinReserv($branchid, $startdate, $enddate, $db) 
{ 
$status = "active"; 
$this->$db->trans_start(); 
$this->$db->select('reservationid, startdate, enddate, hotelbrancheshasreservations.status'); 
$this->$db->from('reservation'); 

$this->$db->where('("'. $startdate . '" BETWEEN startdate AND enddate ) OR ("'. $enddate . '" BETWEEN startdate AND enddate)'); 
$this->$db->where('(startdate BETWEEN "' . $startdate . '" AND "' . $enddate . '") OR (enddate BETWEEN "' . $startdate . '" AND "' . $enddate . '")'); 
$this->$db->join('hotelbrancheshasreservations', 'hotelbrancheshasreservations.reservations_reservationsid = reservation.reservationid'); 
$this->$db->where('(hotelbrancheshasreservations.status = '."'".$status."')"); 

$this->$db->where('(hotelbrancheshasreservations.hotelbranches_hotelbranchesid ='."'".$branchid."')"); 

$result = $this->$db->get()->result_array(); 
$this->$db->trans_complete();     
return $result; 
} 

如果我提供startdate 2016-05-24enddate 2016-05-26,然後它會選擇保留其如下:

[{"reservationid":"KHAN2016Q224","startdate":"2016-05-23","enddate":"2016-05-27","status":"pending"}] 

此預訂應跳過因爲它在表「hotelbrancheshasreservations」

它選擇那些保留這是在提供的日期內,但不活躍。我想選擇那些在提供日期有效並可用的預訂。

回答

1

你的說法,現在將轉化爲

select reservationid, startdate, enddate, hotelbrancheshasreservations.status 
from reservation 
join hotelbrancheshasreservations 
on hotelbrancheshasreservations.reservations_reservationsid 
    = reservation.reservationid 
where ("$startdate" BETWEEN startdate AND enddate) 
    OR ("$enddate" BETWEEN startdate AND enddate) 
    AND (startdate BETWEEN "$startdate" AND "$enddate") 
    OR (enddate BETWEEN "$startdate" AND "$enddate") 
    AND (hotelbrancheshasreservations.status = '$status') 
    AND (hotelbrancheshasreservations.hotelbranches_hotelbranchesid = '$branchid'); 

所以你AND (hotelbrancheshasreservations.status = '$status')將只適用於您的最後一天,部分(enddate BETWEEN "$startdate" AND "$enddate")

您可以使用group_start()/group_end()到周圍添加包含OR S或只需添加它們直接你的兩個選擇括號:

$this->$db->select('reservationid, startdate, enddate, hotelbrancheshasreservations.status'); 
$this->$db->from('reservation'); 

$this->$db->where('(("'. $startdate . '" BETWEEN startdate AND enddate ) 
        OR ("'. $enddate . '" BETWEEN startdate AND enddate))'); 
$this->$db->where('((startdate BETWEEN "' . $startdate . '" AND "' . $enddate . '") 
        OR (enddate BETWEEN "' . $startdate . '" AND "' . $enddate . '"))'); 
$this->$db->join('hotelbrancheshasreservations', 
'hotelbrancheshasreservations.reservations_reservationsid = reservation.reservationid'); 
$this->$db->where('(hotelbrancheshasreservations.status = '."'".$status."')"); 
$this->$db->where('(hotelbrancheshasreservations.hotelbranches_hotelbranchesid 
        ='."'".$branchid."')"); 

並請確保您的字符串是乾淨/逃過一劫,如果您使用自定義像這樣的querys沒有安全activerecord可以提供。