我正在使用CodeIgniter Framework。我希望在特定日期获得所有可用的房间。算法如下:
1)我将给出一个日期来检查房间的可用性。
2)系统应查看表“hotelbrancheshasreservations”,并根据分支ID和status = 'active'.
3)然后选择所有有效的预订并与预订表结合使用。
4)只有那些预订是在加入后从“预订”表中提取的,这些预订在提供的日期没有保留并且是有效的。该表中将有数以千计的预订,但应该跳过。
5)然后,活动预订将与“reservedrooms”表合并,这将提供房间数量。虽然目前已经预订,但可以保留用于与当前预订日期没有冲突的其他日期。
6)最后,目前预订的房间也加入了“酒店房间”表,并且所有可用的房间都会被退回以进行新的预订。
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”
表格中无效选择那些在提供的日期内但未激活的预订。我想选择那些在提供日期有效且可用的预订。
答案 0 :(得分: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."')");
如果你使用这样的自定义查询,请确保你的字符串是干净的/转义的,而没有安全的activerecord可以提供。