如何选择当前日期和时间的分钟间隔范围的记录?

时间:2017-12-10 06:16:18

标签: mysql sql

我有专栏:

date =“2017-12-10”(type = date)

time =“14:10:00”(type = time)

duration =“40”(type = varchar 100)

例如,今天的日期是“2017-12-10”,今天的时间是“14:30”。如何仅在当前时间获取记录,并且仅在“40分钟”的时间间隔内获取记录?所以接受的时间范围是“14:10:00-14:50:00”。

我尝试过这样的事情:

SELECT date, time, duration FROM events WHERE date = CURDATE() AND time >= CURTIME();

但它不会在40分钟内搜索。

谢谢。

2 个答案:

答案 0 :(得分:1)

此?

SELECT date, time, duration FROM events WHERE date = CURDATE() AND time between date_add(CURTIME(), interval -duration minutes) and CURTIME();

答案 1 :(得分:1)

我认为您的问题需要+/-(持续时间/ 2)

select
      date_sub(CONCAT(dt, " ", tim), interval ( cast(duration as unsigned) / 2 ) minute) st
    , date_add(CONCAT(dt, " ", tim), interval ( cast(duration as unsigned) / 2 ) minute) fin
    , d.*
from (
    select curdate() as dt, cast('14:10:00' as time) as tim, '40' as duration union all
    select curdate() as dt, cast('14:10:00' as time) as tim, '60' as duration union all
    select curdate() as dt, cast('14:10:00' as time) as tim, '90' as duration
    ) d
where
    CONCAT(dt, " ", tim) between date_sub(CONCAT(dt, " ", tim), interval ( cast(duration as unsigned) / 2 ) minute) 
                             and date_add(CONCAT(dt, " ", tim), interval ( cast(duration as unsigned) / 2 ) minute)