[英]Getting rows count on inner join rails scope
I have the following scope: 我的范围如下:
scope :billable, -> (range_start = nil, range_end = nil) {
joins(:bids)
.where("auctions.complete = 1 AND auctions.starts_at >= ? AND auctions.starts_at <= ? AND auctions.listing = 0", range_start, range_end)
.group("auctions.id")
}
I am trying to determine how to add a constrain to the where clause that allows me to select where count(bids) > auctions.items_count 我正在尝试确定如何向where子句添加约束,以使我可以选择where count(bids)> auctions.items_count
See http://guides.rubyonrails.org/active_record_querying.html#having 请参阅http://guides.rubyonrails.org/active_record_querying.html#having
Thing to keep in mind in SQL: 在SQL中要记住的一点:
You have a condition on which groups to include in the result, since your condition uses the aggregate function count()
. 您需要确定要在结果中包括哪些组,因为您的条件使用了聚合函数count()
。 So it belongs in the HAVING clause. 因此它属于HAVING子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.