sql - 如何选择过去一小时内创建的所有行?
问题描述
我的查询是
SELECT * FROM email_operation WHERE cdate = CURRENT_DATE and ctime >= (NOW() - INTERVAL '1 hour' )
选择在最后一小时创建的所有行,但它不起作用。它在>=
SELECT * FROM email_operation WHERE cdate = CURRENT_DATE and ctime >= (NOW() - INTERVAL '12 hour' )
> ERROR: operator does not exist: time without time zone >= timestamp with time zone
LINE 1: ..._operation WHERE cdate = CURRENT_DATE and ctime >= (NOW() ...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
> Time: 0.001s
解决方案
显然ctime
是一time
列,因此您无法将其与时间戳(这是错误消息告诉您的内容)进行比较。您需要current_time
改用:
and ctime >= current_time - interval '1 hour'
请注意,这在午夜左右无法正常工作。
似乎您已将日期和时间分成两列(这是一个坏主意),但您可以将它们组合起来比较时间戳:
and cdate + ctime >= current_timestamp - interval '1 hour'
推荐阅读
- vue.js - 如何使用与普通路由不同的模板处理 vue-router 的 404 错误
- javascript - 在我的主要组件中嵌套路由的问题
- java - 我无法处理 A 片段中的 RecyclerView onClick 事件
- html - 将图像高度放大到最大高度的最佳方法是什么?
- c++ - 映射插入(const_iterator 提示,值)用法
- python - Scrapy 推送到 Heroku 得到错误:推送被拒绝,无法编译 Python 应用程序
- powershell - 我想通过检查嵌套文件夹将具有选定扩展名的文件移动到另一个文件夹中。谁能帮助我
- android - 我怎么知道用户是否真的分享了他们的 Facebook 时间线的深层链接?
- c++ - 我的 ArrayList 类重新分配中的分段错误
- docker - 如何更改 OpenShift pod 中的 /etc/resolv.conf 文件