使用这个查询:
users = User.where('confirmed_at is NULL AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL ? days)', 1)
在 mysql 中运行正常,但在 Postgresql 中失败:
PG::SyntaxError: ERROR: syntax error at or near "1" LINE 1: ...AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL 1 day)) ^ : SELECT "users".* FROM "users" WHERE (confirmed_at is NULL AND confirmation_sent_at <= DATE_SUB(NOW(), INTERVAL 1 day))
我试图理解但缺少这里的上下文。为什么整数 1 在此查询中无效?
最佳答案
PostgreSQL 中没有函数DATE_SUB()
,所以无法运行。
这个表达式可以在 Postgres 中使用:
... AND confirmation_sent_at <= (now() - interval '1 day')
或者,如果 confirmation_sent_at
是 date
:
... AND confirmation_sent_at <= (now()::date - 1)
关于MySQL 查询在 PostgreSQL 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19867690/