我想运行MySQL查询,根据人员ID(sid)显示可用的时间段。我发现了几个查询,但似乎没有。我不知道从哪里开始查询。
目标是根据自定义间隔显示时段。 30分钟间隔示例:(8:00,8:30,9:00)特定时间范围。
-OR -
在特定时间范围内显示“免费:从上午8点到下午5点”。
我该如何做到这一点?
CREATE TABLE IF NOT EXISTS `appointments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` int(11) NOT NULL,
`sid` int(11) NOT NULL,
`bid` int(11) NOT NULL,
`start` datetime NOT NULL,
`end` datetime NOT NULL,
`currentTimeStamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`statuscode` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=253 ;
INSERT INTO `appointments` (`id`, `cid`, `sid`, `bid`, `start`, `end`, `currenttimestamp`, `status`) VALUES
(238, 81, 1, 1, '2017-05-29 08:00:00', '2017-05-29 08:15:00', '2017-02-21 17:38:42', 'open'),
(241, 81, 1, 1, '2017-05-29 09:00:00', '2017-05-29 09:15:00', '2017-02-21 17:38:42', 'open'),
(245, 81, 2, 1, '2017-05-29 08:00:00', '2017-05-29 08:15:00', '2017-02-21 17:38:42', 'open'),
(246, 81, 2, 1, '2017-05-29 09:00:00', '2017-05-29 09:45:00', '2017-02-21 17:38:42', 'open'),
(247, 81, 2, 1, '2017-05-29 12:00:00', '2017-05-29 12:45:00', '2017-02-21 17:38:42', 'open'),
(250, 81, 3, 1, '2017-05-29 09:00:00', '2017-05-29 09:45:00', '2017-02-21 17:38:42', 'open');
SELECT id, staff, business, free_from, free_until
FROM (
SELECT a.id AS id, a.sid AS staff, a.bid AS business, a.end AS free_from,
(SELECT MIN(c.start)
FROM appointments c
WHERE c.start > a.end) as free_until
FROM appointments a
WHERE NOT EXISTS (
SELECT 1
FROM appointments b
WHERE b.start BETWEEN a.end AND a.end + INTERVAL 60 MINUTE
)
AND a.end BETWEEN '2017-05-29 08:00:00' AND '2017-05-29 17:00:00'
) AS timeslots
WHERE staff = 2
ORDER BY free_until