【发布时间】:2010-12-27 23:42:18
【问题描述】:
我有一个保持高分的 Rails 3 应用程序。我将它托管在使用 postgresql 作为数据库的 Heroku 上。
我需要从分数表中提取最高分。该表包含score 和user_id 列。它在 mysql 中使用以下内容:
Score.order('score DESC').group('user_id').limit(25)
这对每个用户的最高分进行排名。
当我将应用程序放在 Heroku 上时,我收到以下 psql 错误 PGError: ERROR: column "scores.id" must appear in the GROUP BY clause or be used in an aggregate function
我已经阅读了一遍,但没有找到明确的答案。重新创建上述查询以使用 PostgreSQL 的最佳方法是什么?
谢谢!
提姆
【问题讨论】:
-
我正在尝试使用其他人所说的 DISTINCT ON。 Score.select('DISTINCT ON (user_id) id, user_id, score').order('user_id, score DESC').limit(25) 我收到以下错误:“PGError: ERROR: column id_list.alias_0 does not存在第 1 行:...作为 id_list ORDER BY id_list.al..."。我什至没有说明名称为“id_list”的列,所以我不明白为什么会出现这种情况。
标签: ruby-on-rails postgresql ruby-on-rails-3