在Rails中梳理两个SQL where语句

时间:2017-01-01 17:21:00

标签: sql ruby-on-rails activerecord

如何让这两个WHERE语句满意?

where("cast(strftime('%d', date) as int) = ?", month)
where("cast(strftime('%Y', date) as int) = ?", year)

类似的东西:

where("cast(strftime('%d', date) as int) = ?", month) AND
       where("cast(strftime('%Y', date) as int) = ?", year)

我尝试了一些方法,但无法使其正常工作。

由于

1 个答案:

答案 0 :(得分:1)

您可以使用多个问号:

where("cast(strftime('%d', date) as int) = ? 
       AND cast(strftime('%Y', date) as int) = ?", month, year)

或者只使用2个where语句:

where("cast(strftime('%d', date) as int) = ?", month)
.where("cast(strftime('%Y', date) as int) = ?", year)