您可以使用to_char
将时间戳转换为适当的字符串形式并同时去除小数秒:
where to_char(my_time, 'HH24:MM:SS') = '12:00:00'
...
ActiveRecord 版本非常简单:
MyTable.where("to_char(my_time, 'HH24:MM:SS') = '12:00:00'")
然后链入您现有的 BETWEEN 检查。
您还可以使用extract
分别检查每个时间组件:
where extract(hour from my_time) = 12
and extract(minute from my_time) = 0
and extract(second from my_time) = 0
...
ActiveRecord 版本将是:
MyTable.where('extract(hour from my_time) = ?', 12)
.where('extract(minute from my_time) = ?', 0)
.where('extract(second from my_time) = ?', 0)
您必须询问 EXPLAIN 以查看哪个版本效果最好。