简体   繁体   English

Ruby on Rails查询帮助

[英]Ruby on Rails Query Help

I currently have the following query 我目前有以下查询

User.sum(:experience, :group => "clan", :conditions => ["created_at >= ? and created_at <= ?", "2010-02-15", "2010-02-16"])

I want to return the top 50 clans in terms of experience listed from most experience to least experience with only the top 50 in experience returning. 我想返回经验最多的50个氏族,从经验最多的到经验最少的,返回经验最多的50个。 How would I modify the query to achieve that result. 我将如何修改查询以实现该结果。 I know I'll need :limit => 50 to limit the query but if I add :order => "clan DESC" I get the error column "users.experience" must appear in the GROUP BY clause or be used in an aggregate function 我知道我需要:limit => 50来限制查询,但是如果我添加:order =>“ clan DESC”,我会得到错误列“ users.experience”必须出现在GROUP BY子句中或用于聚合功能

您需要在order子句中重复计算

User.sum(:experience, :group => "clan", :order=> "sum(experience) DESC", :limit => 50, :conditions => ["created_at >= ? and created_at <= ?", "2010-02-15", "2010-02-16"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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