使用此查询:
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在此查询中无效?
答案 0 :(得分:3)
PostgreSQL中没有函数DATE_SUB()
,所以它无法工作。
这个表达式适用于Postgres:
... AND confirmation_sent_at <= (now() - interval '1 day')
或者,如果confirmation_sent_at
是date
:
... AND confirmation_sent_at <= (now()::date - 1)
答案 1 :(得分:1)
...试
users = User.where(
"confirmed_at IS NULL " +
"AND confirmation_sent_at <= (NOW() - INTERVAL '1 DAY')"
)