这是我的代码,错误在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)
答案 0 :(得分:1)
根据您的解释性说明,您可以像这样编写查询:
query = query.where(<<-EOQ, first_of_month, last_of_month, today)
close_date BETWEEN ? AND ?
AND (quote_exp >= ? OR status_id = 8)
EOQ
这将为您提供:(1)月份(2)中未过期或状态为8的所有内容8.请注意A->B
也是~AvB
。
答案 1 :(得分:0)
您的第4行不应包含WHERE
关键字。如果您打算将其他标准添加到其余标准,则应将其替换为AND
,如下所示:
query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 AND quote_exp < ? ', first_of_month, last_of_month, today)
答案 2 :(得分:0)
你可以用这种方式写得更好。
query = quotes.where(close_date: first_of_month..last_of_month, status_id: 8).where('quote_exp < ?', today).sum(:total)