提问者:小点点

将硬编码查询从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中不存在,您知道有什么替代方法吗?

谢谢


共1个答案

匿名用户

在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上的索引来提高性能。