如何使用Rails在PostgreSQL中处理GROUP BY? [英] How to approach GROUP BY in PostgreSQL using Rails?
问题描述
我有一个保持高分的Rails 3应用程序。我将其托管在使用Postgresql作为数据库的Heroku上。
I have a Rails 3 app which keeps high scores. I'm hosting it on Heroku which uses postgresql as the db.
我需要从成绩表中提取最高成绩。该表具有列得分
和 user_id
列。它在mysql中使用以下代码:
I need to extract the top scores from the scores table. The table has columns score
and user_id
. It was working in mysql with the following:
Score.order('score DESC')。group('user_id')。limit(25 )
这会排名每个用户的最高得分。
This ranks each user's top score.
当我放置应用程序时在Heroku上,出现以下psql错误 PGError:错误:列 scores.id必须出现在GROUP BY子句中或在聚合函数中使用
When I put the app on Heroku, I get the following psql error PGError: ERROR: column "scores.id" must appear in the GROUP BY clause or be used in an aggregate function
我读过一些,但没有找到明确的答案。重新创建上述查询以与PostgreSQL一起使用的最佳方法是什么?
I've read around but haven't found a clear answer. What is the most optimal way to recreate the above query to work with PostgreSQL?
谢谢!
Tim
推荐答案
这意味着您的选择查询正在选择 id列,但未将其包含在group by子句中。我不熟悉Rails,但是它会选择*还是所有列?
That means your select query is selecting the "id" column but not including it in the group by clause. I'm not familiar with rails but could it be selecting * or all columns?
这篇关于如何使用Rails在PostgreSQL中处理GROUP BY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!