如何在where子句中组合两个条件? [英] How to combine two conditions in a where clause?

查看:822
本文介绍了如何在where子句中组合两个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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

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

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

How can I add to the where clause with a statement like:

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

如何将两者结合?谢谢

推荐答案

如果您想使用 AND条件查询,请尝试以下操作:

if you want a "AND" conditional query, try this:

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

其中会产生类似如下的SQL: select ... where ... AND ...

which will produce SQL like : select ... where ... AND ...

如果您需要WHERE子句更复杂,例如:其中(a AND b)或(c AND d),您必须自己将条件合并到子句中,例如

if you want the WHERE clause more complicated, such as: where ( a AND b) OR (c AND d), you have to combine the conditions into the clause yourself, e.g.

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

这篇关于如何在where子句中组合两个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆