如何在where子句中组合两个条件?

时间:2012-05-16 03:57:23

标签: ruby-on-rails ruby-on-rails-3 activerecord

我有以下内容:

time_range = (1.month.ago.beginning_of_month..1.month.ago.end_of_month)

Comment.where(:created_at => time_range).count

如何使用如下语句添加到where子句:

.where("user_id is not in (?)",[user_ids]).

我如何将两者结合起来?感谢

3 个答案:

答案 0 :(得分:65)

如果你想要“AND”条件查询,试试这个:

Comment.
  where(:created_at => time_range).
  where("user_id is not in (?)",[user_ids])

将产生类似于select ... where ... AND ...

的SQL

如果您希望WEHRE子句更复杂,例如:where ( a AND b) OR (c AND d),您必须自己将条件组合到子句中,例如

Comment.where("(a AND b ) OR (c AND d)")

答案 1 :(得分:17)

User.where(["name = ? and email = ?", "Joe", "joe@example.com"])

这没关系。

答案 2 :(得分:12)

Public Sub DeleteFormQueries()
    Dim qdf As Object
    For Each qdf In CurrentDb.QueryDefs
        If qdf.Name Like "~sq_f*" Then
            CurrentDb.QueryDefs.Delete qdf.Name
        End If
    Next qdf
End Sub
相关问题