开发者

ActiveRecord Query

Anyone know how I can create this query using ActiveRecord rather than using SQL?

@leaderboard = ActiveRecord::Base.connection.execute("SELECT name, street, town, county,        avg(volume_used)  FROM `DBName`.`events
join DBName.households on idhouseholds = events.household_id 
group by household_id
order by v开发者_如何学Pythonolume_used ASC;");


How about...

@leaderboard = Event.select([:name,:street,:town,:county,'avg(volume_used) as avg_used']).joins(:households).group('events.household_id').order('volume_used asc')

assuming that by idhouseholds you actually meant households.id and that you want to order by the aggregate average field you are selecting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜