【发布时间】:2019-10-15 19:15:34
【问题描述】:
我有一个datetime 专栏。如何获取 Date 列在两个特定小时之间的行(不考虑日、月和年)。
例如,小时在14:00:00 和15:00:00 之间的任何行(全天)?
【问题讨论】:
我有一个datetime 专栏。如何获取 Date 列在两个特定小时之间的行(不考虑日、月和年)。
例如,小时在14:00:00 和15:00:00 之间的任何行(全天)?
【问题讨论】:
您可以使用time() 或hour():
where hour(datetime) >= 14 and hour(datetime) < 16
或:
where time(datetime) >= '14:00:00' and time(datetime) < '16:00:00'
或者你可能更喜欢这样:
where time(datetime) >= '14:00:00' and
time(datetime) < '15:00:00' + interval 1 hour
注意:由于datetime 列上的函数调用,这些都不会使用索引。
【讨论】:
试试这个
select * from table where hour(datetime) between 14 and 16
或
select * from table where time(datetime) between '14:00:00' and '16:00:00'
【讨论】:
select *
from (
select cast (substr(to_char(datetime,'YYYY-mm-dd HH:MM:SS'),12,2) as integer ) as compare
from table )
where compare between 14 and 15;
【讨论】: