两个日期范围,如果它在另一个两个日期范围之间

时间:2016-08-01 10:13:23

标签: mysql

我想根据其他开始日期和日期结束选择输入日期范围之间的所有记录,但我的查询不返回结果

下面的示例输出,因为它仍然在该datestart和dateend之间

------------------------------------------------------------------------------------------
 PK_schedules | PatientName | DateTimeStarted     | DateTimeEnd         | RoomCode |RoomNo
------------------------------------------------------------------------------------------
    1001      | EMMAN SALAS | 2016-08-01 15:30:00 | 2016-08-01 18:30:00 | 1003     | Room 3

这是我的查询

SELECT 
    a.PK_schedules, 
    CONCAT(d.lastname,', ',d.firstname,' ',d.middlename) `PatientName`, 
    TIME(b.description) `TimeStarted`, 
    TIME(c.description) `TimeEnd`, 
    b.PK_time `TimeStartCode`, 
    c.PK_time `TimeEndCode`, 
    TIMESTAMP(a.datestarted,b.description) `DateTimeStarted`,
    TIMESTAMP(a.dateend,c.description) `DateTimeEnd`, 
    e.PK_rooms `RoomCode`, 
    e.description `RoomNo` 
FROM schedules a 
INNER JOIN time b ON b.PK_time = a.timestart 
INNER JOIN time c ON c.PK_time = a.timeend 
INNER JOIN datacenter d ON d.PK_datacenter = a.FK_patients 
INNER JOIN rooms e ON e.PK_rooms = a.FK_rooms 
WHERE 
    (
        (TIMESTAMP(a.datestarted,b.description)  BETWEEN 
                '2016-08-01 12:00:00' AND 
                '2016-08-01 15:30:00') OR
        (TIMESTAMP(a.dateend,c.description)BETWEEN 
                '2016-08-01 12:00:00' AND 
                '2016-08-01 15:30:00')
    ) AND
    a.cancelflag <> '1' AND 
    a.FK_rooms = '1003'

0 个答案:

没有答案