我有一张表time_period
,
CREATE TABLE `time_period` (
`id` int(11) NOT NULL,
`time` time NOT NULL,
`slots` int(11) NOT NULL
) ;
表中的数据基本上是24小时制中的时间段,间隔为5分钟。像这样:
INSERT INTO `time_period` (`id`, `time`, `slots`) VALUES
(1, '00:00:00', 3),
(2, '00:05:00', 3),
(3, '00:10:00', 3),
(289, '24:00:00', 3);
实时测试:http://rextester.com/live/UXPV82172
我想要实现的是,在min_time和max_time之间获得一小时的时间(我在min_time和max_time中给出的日期连接相同的日期),给定current_time(实际当前日期和时间)。
SELECT
t0_.id AS id_0,
t0_.time AS time_1,
t0_.slots AS slots_2
FROM time_period t0_ WHERE
'2018-03-18' + t0_.time >= '2018-03-18 19:09' AND # :min_time
'2018-03-18' + t0_.time <= '2018-03-18 20:09' AND # :max_time
'2018-03-18' + t0_.time >= '2018-03-17 21:05' # :current_time
ORDER BY t0_.time ASC LIMIT 20
看起来很直接,但出于某种原因,我得到的是:
1,00:00:00, 3
预期输出必须与运行sql时相同:
SELECT
t0_.id AS id_0,
t0_.time AS time_1,
t0_.slots AS slots_2
FROM time_period t0_ WHERE
t0_.time >= '19:09:00' AND
t0_.time <= '20:09:00';
即。
1 245 19:10:00 3
2 246 19:15:00 3
3 247 19:20:00 3
...等等
请有人帮我解释一下。
答案 0 :(得分:1)
如果要比较它们,则需要将它们构建为日期时间。
CONVERT(DATE, '2018-03-18') +
t0_.time >= CONVERT(DATETIME,
'2018-03-18 19:09') AND # :min_time
#etc
答案 1 :(得分:0)
显然我只需要连接我的日期和时间字段来获取datetime字段以进行比较。
SELECT
t0_.id AS id_0,
t0_.time AS time_1,
t0_.slots AS slots_2,
concat('2018-03-18', " ", t0_.time) as x
FROM time_period t0_ WHERE
concat('2018-03-18', " ", t0_.time) >= '2018-03-18 19:09:00'
AND
concat('2018-03-18', " ", t0_.time) <= '2018-03-18 20:09:00'
AND
concat('2018-03-18', " ", t0_.time) >= '2018-03-17 21:05'
ORDER BY t0_.time ASC LIMIT 50