我正在尝试创建查询以列出特定日期的开放时间段。我在这里发现了许多类似的问题,但似乎没有答案。
我预定的约会表的简化版本:
表名称: app_bookings (在输出之前)
CREATE TABLE `app_bookings` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`client_id` INT(11) NOT NULL DEFAULT '0',
`provider_id` INT(11) NOT NULL DEFAULT '0',
`doctor_id` INT(11) NOT NULL DEFAULT '0',
`department_id` INT(11) NOT NULL DEFAULT '0',
`date` DATE NOT NULL,
`StartTime` DATETIME NULL DEFAULT NULL,
`EndTime` DATETIME NULL DEFAULT NULL,
`duration` INT(11) NOT NULL,
`payment` VARCHAR(50) NOT NULL,
`created_by` INT(11) NOT NULL,
`updated_by` INT(11) NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `app_bookings` ( `client_id`, `provider_id`, `doctor_id`, `department_id`, `date`, `StartTime`, `EndTime`, `duration`, `payment`, `created_by`, `updated_by` ) VALUES
( 1, 3, 11, 1, '2018-10-21', '2018-10-21 10:15:00', '2018-10-21 11:00:00', 0, '', 0, 0 ),
( 1, 3, 11, 1, '2018-10-21', '2018-10-21 11:15:00', '2018-10-21 13:00:00', 0, '', 0, 0 ),
( 1, 3, 11, 1, '2018-10-21', '2018-10-21 14:15:00', '2018-10-21 14:45:00', 0, '', 0, 0 ),
( 1, 3, 11, 1, '2018-10-21', '2018-10-21 15:00:00', '2018-10-21 15:45:00', 0, '', 0, 0 ),
( 1, 3, 11, 1, '2018-10-21', '2018-10-21 16:00:00', '2018-10-21 16:30:00', 0, '', 0, 0 );
我当前的MYSQL查询 :(我从另一个答案中拉出并进行了更改以供使用)
SELECT AvailStartTime, AvailEndTime
FROM (SELECT @lastEndTime as AvailStartTime, StartTime as AvailEndTime,
@lastEndTime := EndTime
FROM (SELECT StartTime, EndTime
FROM app_bookings
WHERE doctor_id = 14
AND EndTime >= '2018-10-21 10:00'
AND StartTime < '2018-10-21 19:00'
ORDER BY StartTime
) e
JOIN (SELECT @lastEndTime := NULL) init) x WHERE
AvailEndTime > DATE_ADD(AvailStartTime, INTERVAL 15 MINUTE)
以下是上述查询的输出:
我使用此查询获得正确的输出,但我需要跳过医生的午餐时间,午餐开始时间为@startlunch“ 13:00”至“ 14:00” 并获得了从2018-10-21 15:30:00到2018-10-21 19:00的记录(如果预订了任何条目,则跳过)