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

编程入门 行业动态 更新时间:2024-10-21 11:28:06
本文介绍了如何在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子句中组合两个条件?

本文发布于:2023-10-26 23:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   句中   条件   两个   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!