SQL Statement / Codeigniter预约

时间:2017-06-18 21:30:28

标签: php mysql sql codeigniter appointment

我正在制作带日历的预订软件。我有四个表格如下:

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-06-18 01:30'
结束日期' 2017-06-18 02:30'

结果:

EmployeeID {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;
}

2 个答案:

答案 0 :(得分:0)

也许您可以在两个不同的查询中轻松分割问题,

首先,您可以编写一个匹配您要排除的所有内容的查询, 我们称之为错误结果, 之后,您可以从员工客房表中获取不在 错误结果的所有员工和客房

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)));

答案 1 :(得分:0)

使用SQL语句:

SELECT `Employees`.`EmployeeID`, `Employees`.`Name`
FROM (`Employees`)
WHERE `Employees`.`EmployeeID` NOT IN (SELECT Services.EmployeeID FROM Services where 
 (( yourstartdate BETWEEN Services.StartDate AND Services.EndDate) or 
 ( yourenddate BETWEEN Services.StartDate AND Services.EndDate) or
 (Services.StartDate BETWEEN yourstartdate AND yourenddate) or
 (Services.EndDate BETWEEN yourstartdate AND yourenddate)) union
 (SELECT EMPBREAKS.EMPID FROM EMPBREAKS WHERE(
 (yourstartdate BETWEEN EMPBREAKS.StartB AND EMPBREAKS.EndB) or 
 (yourenddate BETWEEN EMPBREAKS.StartB AND EMPBREAKS.EndB) or 
 (EMPBREAKS.StartB BETWEEN yourenddate AND yourstartdate) or
 (EMPBREAKS.EndB BETWEEN yourenddate AND yourstartdate))))

AND

SELECT `Rooms`.`RoomID`, `Rooms`.`Name`
FROM (`Rooms`)
WHERE `Rooms`.`RoomID` not in (SELECT Services.RoomID FROM Services WHERE 
 ((Services.startdate BETWEEN 'yourstartdate AND yourenddate) OR 
 (Services.enddate BETWEEN 'yourstartdate AND yourenddate) OR 
 ('yourstartdate BETWEEN Services.startdate AND Services.enddate) OR
 (yourenddate BETWEEN Services.startdate AND Services.enddate)))