MySQL查询在PostgreSQL中失败

时间:2013-11-08 20:08:10

标签: mysql sql ruby-on-rails postgresql migrate

使用此查询:

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在此查询中无效?

2 个答案:

答案 0 :(得分:3)

PostgreSQL中没有函数DATE_SUB(),所以它无法工作。

这个表达式适用于Postgres:

... AND confirmation_sent_at <= (now() - interval '1 day')

或者,如果confirmation_sent_atdate

... AND confirmation_sent_at <= (now()::date - 1)

答案 1 :(得分:1)

...试

users = User.where(
    "confirmed_at IS NULL " +
    "AND confirmation_sent_at <= (NOW() - INTERVAL '1 DAY')"
)