我正在使用每日日曆的預約軟件。我有四個表如下:SQL Statement/Codeigniter預約
Services:
ID | EmployeeID| ServiceID| RoomID| StartDate | EndDate
1 | 1 | 1 | 2 | 2017-06-18 01:00 |2017-06-18 02:00
2 | 2 | 1 | 1 | 2017-06-18 03:00 |2017-06-18 04:00
Employees:
EmployeeID | Name
1 | A
2 | B
3 | C
Rooms:
RoomID | Name
1 | X
2 | Y
3 | Z
EmpBreaks:
ID | EmployeeID | BreakStart | BreakEnd
1 | 1 | 2017-06-18 02:00 | 2017-06-18 03:00
凡服務表存儲預訂的約會細節,僱員表包含了所有員工的列表,客房表包含了所有房間的列表,以及EmpBreaks存儲所有時間當僱員不可用時。
我想執行一個查詢來搜索指定開始日期和結束日期的所有可用員工和房間。
例如:
開始日期 '2017年6月18日01:30'
結束日期 '2017年6月18日02:30'
結果:
僱員{3- }
RoomID {1,3}
在這裏被更新的工作代碼。
function check_available_rooms_therapists() {
$this->data['message'] = array();
$this->data['success'] = FALSE;
$post = $this->input->post();
$EmployeeFields = array(
'Employees.EmployeeID',
'Employees.Name'
);
$sDate = $post['StartDate'];
$eDate = $post['EndDate'];
$whereEmp = "Employees.EmployeeID NOT IN (SELECT Services.EmployeeID FROM Services where
(('" . $sDate . "' BETWEEN Services.StartDate AND Services.EndDate) or
('" . $eDate . "' BETWEEN Services.StartDate AND Services.EndDate) or
(Services.StartDate BETWEEN '". $sDate ."' AND '" . $eDate ."') or
(Services.EndDate BETWEEN '". $sDate ."' AND '" . $eDate ."')) union
(SELECT EMPBREAKS.EMPID FROM EMPBREAKS WHERE(
('" . $sDate . "' BETWEEN EMPBREAKS.StartB AND EMPBREAKS.EndB) or
('" . $eDate . "' BETWEEN EMPBREAKS.StartB AND EMPBREAKS.EndB) or
(EMPBREAKS.StartB BETWEEN '" . $eDate . "' AND '" . $sDate ."') or
(EMPBREAKS.EndB BETWEEN '" . $eDate . "' AND '" . $sDate ."'))))";
$this->db->select($EmployeeFields);
$listing = $this->db->get_where('Employees', $whereEmp, 50 , 0);
$result = $listing -> result();
$avEmpName = array();
$avEmpID = array();
foreach ($result as $row) {
$avEmpID[] = $row->EmployeeID;
$avEmpName[] = $row->Name;
}
$RoomFields = array(
'Rooms.RoomID',
'Rooms.Name'
);
$whereRoom = "Rooms.RoomID not in (SELECT Services.RoomID FROM Services WHERE
((Services.startdate BETWEEN '" . $sDate . "' AND '" . $eDate. "') OR
(Services.enddate BETWEEN '" . $sDate . "' AND '" . $eDate. "') OR
('" .$sDate . "' BETWEEN Services.startdate AND Services.enddate) OR
('" .$eDate . "' BETWEEN Services.startdate AND Services.enddate)))";
$this->db->select($RoomFields);
// $this->db->from('Rooms');
$Rooms = $this->db->get_where('Rooms', $whereRoom, 50 ,0);
$Rresult = $Rooms -> result();
$avRoomName = array();
$avRoomID = array();
foreach ($Rresult as $r) {
$avRoomID[] = $r->RoomID;
$avRoomName[] = $r->Name;
}
$this->data['EmployeeID'] = $avEmpID;
$this->data['RoomID'] = $avRoomID;
$this->data['RoomName'] = $avRoomName;
$this->data['Name'] = $avEmpName;
$this->data['success'] = TRUE;
echo json_encode($this->data);
die;
}
編輯你的問題,並提供樣本數據和預期的結果。 –