mysql - 将硬编码查询从 MYSQL 迁移到 PostgreSQL
问题描述
嗨,我有一个看起来像这样的“mysql”查询:
... WHERE (maintenanceLockTimestamp + "+ delay+" SECOND) < CURRENT_TIMESTAMP ) ) "
问题是,我们需要从 MYSQL 转移到 PostgreSQL,但是在执行相同的查询时,我得到:
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "SECONDS"
我在这里检查:https ://www.postgresqltutorial.com/postgresql-date-functions/
看起来 PostgreSQL 中不存在 SECOND 函数,你知道有什么替代方法吗?
谢谢
解决方案
在 Postgres 中,您可以使用:
where maintenanceLockTimestamp + :delay * interval '1 second' < current_timestamp
请注意使用参数来避免将查询字符串与文字值混淆。
在这两个数据库中,最好使用以下日期/时间算法编写current_timestamp
:
where maintenanceLockTimestamp < current_timestamp - ? second -- MySQL
where maintenanceLockTimestamp < current_timestamp - :delay * interval '1 second' -- Postgres
这允许优化器使用索引来maintenanceLockTimestamp
提高性能。
推荐阅读
- android - 如何创建可访问的 PrivateKeys?
- julia - Julia函数将秒转换为小时、分钟、秒的问题
- javascript - Next.js Router.push 没有设置任何 req.headers
- azure - CI/CD 时无法在 Azure 上复制文件和文件夹
- c++ - 为单个函数一次声明多个变量的最佳方法是什么?
- html - 在不同大小的网格中移动按钮
- react-native - 修改index.js后黑屏
- c# - MVC 5 AJAX 代码未访问控制器类操作服务器端
- python - Google Cloud - 收到无效的 JSON 负载。'config' 中的未知名称“编码”:原始字段不重复,无法启动列表
- c# - 如何通过在自托管应用程序中使用 HTTPListener 托管的 HTML 页面下载文件