简体   繁体   English

获取行取决于内部连接导轨范围

[英]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中要记住的一点:

  • The WHERE clause is for conditions on individual rows, before they have been put into groups by a GROUP BY. WHERE子句用于单个行上的条件, 然后由GROUP BY将它们分组。
  • The HAVING clause is for conditions on groups. HAVING子句适用于组上的条件。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM