繁体   English   中英

CodeIgniter SQL查询未返回逻辑结果

[英]CodeIgniter SQL Query Not Returning Logical Result

酒店客房预订ER图

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

1)我将安排日期检查房间的可用性。

2)系统应查看“ hotelbrancheshasreservations”表,并应根据分支ID和status = 'active'.选择预订ID status = 'active'.

3)然后,选择所有活动的预订并将其与预订表合并。

4)只有那些保留在联接后从“保留”表中提取的保留 ,这些保留在提供的日期没有保留并且是活动的。 该表中将有成千上万的预订,但是应该跳过。

5)然后将有效的预订与“ reservedrooms”表合并在一起,这将提供房间号。 虽然目前已保留,但可以保留一些其他日期,这些日期与当前保留日期没有冲突。

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”中未激活

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

您现在的陈述将转换为

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

如果使用像这样的自定义查询而没有安全性ActiveRecord可以提供的信息,请确保您的字符串干净/转义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM