我正在尝试找一天的空闲时段,但我坚持正确的查询。 开放时间或每天在每天的9:00到22:00之间运行,以下查询在当天返回正确的数据,除非当天的第一个预订是10:00,它将不会返回9之间的未预订的时段:00:00和10:00,如果从21:30到21:40预订,它将不会显示当天的最后一个免费时段。
在每分钟绕过这个并且set_id我必须在插入中创建一天开始和结束1分钟的条目,作为id = 1和id = 6。
我正试图摆脱每天和每个set_id
创建这两个插入CREATE TABLE bookings
(`id` int, `time_from` datetime, `time_to` datetime, `set_id` int);
INSERT INTO bookings
(`id`, `time_from`, `time_to`, `set_id`)
VALUES
(1, '2013-11-20 08:59:00', '2013-11-20 09:00:00', 6),
(2, '2013-11-20 09:10:00', '2013-11-20 10:00:00', 6),
(3, '2013-11-20 11:10:00', '2013-11-20 11:30:00', 6),
(4, '2013-11-20 12:00:00', '2013-11-20 12:40:00', 6),
(5, '2013-11-20 16:20:00', '2013-11-20 16:50:00', 6),
(6, '2013-11-20 22:00:00', '2013-11-20 22:01:00', 6)
;
SELECT Available_from, Available_to
FROM (
SELECT @lasttime_to AS Available_from, time_from AS Available_to, @lasttime_to := time_to
FROM (SELECT time_from, time_to
FROM bookings
WHERE set_id = 6
AND time_to >= '2013-11-20 08:59'
AND time_from < '2013-11-20 22:01'
ORDER BY time_from) e
JOIN (SELECT @lasttime_to := NULL) init) x
WHERE Available_to > DATE_ADD(Available_from, INTERVAL 9 MINUTE);
| AVAILABLE_FROM | AVAILABLE_TO |
|---------------------|---------------------------------|
| 2013-11-20 09:00:00 | November, 20 2013 09:10:00+0000 |
| 2013-11-20 10:00:00 | November, 20 2013 11:10:00+0000 |
| 2013-11-20 11:30:00 | November, 20 2013 12:00:00+0000 |
| 2013-11-20 12:40:00 | November, 20 2013 16:20:00+0000 |
| 2013-11-20 16:50:00 | November, 20 2013 22:00:00+0000 |
任何帮助将不胜感激。
http://www.sqlfiddle.com/#!2/b30a0/2
CREATE TABLE `days` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`time_from` datetime NOT NULL,
`time_to` datetime NOT NULL,
`step` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `days` (`id`, `date`, `time_from`, `time_to`, `step`)
VALUES
(1, '2013-11-20', '2013-11-20 09:00:00', '2013-11-20 22:00:00', 10),
(2, '2013-11-21', '2013-11-21 09:00:00', '2013-11-21 22:00:00', 10);
A Little Closer!
INSERT INTO `bookings` (`id`, `date`, `time_from`, `time_to`, `set_id`, `name`, `email`, `telephone`, `amount`, `notes`, `is_paid`, `is_booked`)
VALUES
(25, '2013-11-20', '2013-11-20 09:10:00', '2013-11-20 09:30:00', 1, '', '', '', NULL, NULL, 0, 0),
(26, '2013-11-20', '2013-11-20 10:30:00', '2013-11-20 11:30:00', 1, '', '', '', NULL, NULL, 0, 0),
(27, '2013-11-20', '2013-11-20 12:30:00', '2013-11-20 13:20:00', 1, '', '', '', NULL, NULL, 0, 0),
(29, '2013-11-20', '2013-11-20 15:00:00', '2013-11-20 16:40:00', 1, '', '', '', NULL, NULL, 0, 0);
这就是我得到的
availableFrom availableTo
9:00 2013-11-20 09:10:00
2013-11-20 09:30:00 2013-11-20 10:30:00
2013-11-20 11:30:00 2013-11-20 12:30:00
9:00 2013-11-20 15:00:00
2013-11-20 15:00:00 22:00
答案 0 :(得分:0)
一种可能的选择是将表格左键连接到这些行:
select (case when ltime_to is null then '9:00' else ltime_to end) as availableFrom,
rtime_from as availableTo
from (select l.time_to as ltime_to, r.time_from as rtime_from from bookings r
left join bookings l on l.id=r.id-1) lr
union
select max(time_from) as availableFrom, '22:00' as availableTo from bookings