简体   繁体   English

Rails 4 Postgres查询错误

[英]Rails 4 Postgres query error

Here is my code, error comes back with an error at or near WHERE 这是我的代码,错误在WHERE或附近返回错误

first_of_month = Date.current.beginning_of_month
last_of_month = Date.current.end_of_month
today = Date.current
query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 WHERE quote_exp < ? ', first_of_month, last_of_month, today)

query.sum(:total)

Based on your explanatory comments, you could write the query like this: 根据您的解释性注释,您可以这样编写查询:

query = query.where(<<-EOQ, first_of_month, last_of_month, today)
  close_date BETWEEN ? AND ?
  AND (quote_exp >= ? OR status_id = 8)
EOQ

That will give you: (1) everything from the month (2) that is either not expired or has status 8. Remember that A->B is also ~AvB . 这将为您提供:(1)从月份(2)开始的所有未到期或具有状态8的内容。请记住, A->B也是~AvB

Your 4th line should not contain the WHERE keyword. 您的第四行不应包含WHERE关键字。 You should replace it with AND if your intent is to add the other criterion to the rest, like so: 如果打算将其他条件添加到其余条件,则应将其替换为AND ,如下所示:

query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 AND quote_exp < ? ', first_of_month, last_of_month, today)

您可以通过这种方式更好地编写它。

query = quotes.where(close_date: first_of_month..last_of_month, status_id: 8).where('quote_exp < ?', today).sum(:total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM