我正在开发一个带有每日日历的预订软件。我有四个表如下:
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
Services 表存储预订约会的详细信息,Employees 表包含所有员工的列表,Rooms 表包含所有房间的列表,EmpBreaks 存储员工不可用的所有时间。
我想执行查询以在指定的开始和结束日期搜索所有可用的员工和房间。
例如:
开始日期'2017-06-18 01:30'
结束日期 '2017-06-18 02:30'
结果:
员工 ID {3}
房间 ID {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;
}
最佳答案
也许您可以在两个不同的查询中轻松地拆分您的问题,
首先,你可以写一个匹配所有你想要排除的查询, 我们称它为 wrong result , 之后,您可以从 Employee 和 Rooms 表中获取所有不在 错误结果中的员工和房间
select * from Employees
where Employees.EmployeeID not in (
SELECT a.EmployeeID
FROM Appointments a
where ((a.startdate BETWEEN yourstartdate AND yourenddate ) or (a.enddate BETWEEN yourstartdate AND yourenddate)));
select * from Rooms
where Rooms.RoomID not in (
SELECT a.RoomID
FROM Appointments a
where ((a.startdate BETWEEN yourstartdate AND yourenddate ) or (a.enddate BETWEEN yourstartdate AND yourenddate)));
关于php - SQL语句/Codeigniter预约,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44619714/