简体   繁体   English

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

[英]How to select records with minutes interval range of current day and time?

I have columns: 我有专栏:

date = "2017-12-10" (type = date) 日期=“ 2017-12-10”(类型=日期)

time = "14:10:00" (type = time) 时间=“ 14:10:00”(类型=时间)

duration = "40" (type = varchar 100) 持续时间=“ 40”(类型= varchar 100)

For example, today date is "2017-12-10" and today time is "14:30". 例如,今天日期是“ 2017-12-10”,今天时间是“ 14:30”。 How can I get records only for current time and only in time interval "40 minutes"? 如何只获取当前时间和“ 40分钟”时间间隔内的记录? So accepted time range would be "14:10:00-14:50:00". 因此,可接受的时间范围为“ 14:10:00-14:50:00”。

I've tried something like this: 我已经尝试过这样的事情:

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

But it doesn't search in 40 minutes interval. 但它不会在40分钟的间隔内进行搜索。

Thank you. 谢谢。

这个?

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

I thought your problem required a need for +/-(duration/2) 我认为您的问题需要+/-(持续时间/ 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM