使用括号对 OR 条件进行分组,以保留 CodeIgniter 查询中的逻辑

问题描述 投票:0回答:1

Hotel Rooms Reservation ER Diagram

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

  1. 我将给出一个日期来检查房间的可用性。

  2. 系统应查看表“hotelbrancheshasreservations”,并应根据分店 ID 和 status = 'active'.

     选择
    预订 id

  3. 然后选择所有活动的预订并与预订表连接。

  4. 仅在加入后从“预订”表中提取那些在提供的日期未预订且处于活动状态的预订。此表中会有数千个预订,但应该跳过。

  5. 然后将有效预订与“预订房间”表连接起来,这将提供房间号。虽然目前已预订,但可以预订其他与当前预订日期不冲突的日期。

  6. 最后,当前预订的房间也加入到“hotelrooms”表中,并返回所有可用房间以供新预订。

  7. 所以一个房间可以多次预订,但日期不同。

如果我提供像

$startdate = 2016-05-16
$enddate = 2016-05-27
这样的日期,我无法在 CodeIgniter 中开发适当的查询来获取所有可用房间。

MySQL Db 中的数据类型

date

我正在 CodeIgniter

中运行以下查询
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-24
enddate 2016-05-26
,那么它会选择如下预订:

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

应跳过此预订,因为它在表中未激活 “hotelbrancheshasreservations”

它选择那些在提供的日期内但不活跃的预订。我想选择那些在指定日期有效且可用的预订。

php mysql codeigniter query-builder logical-grouping
1个回答
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
的两个选择周围添加括号,或者直接添加它们:

$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."')");

如果您使用这样的自定义查询而没有安全活动记录可以提供,请确保您的字符串是干净的/转义的。

© www.soinside.com 2019 - 2024. All rights reserved.