欢迎.....
我想在我的rails应用上获得最多用户帖子
@section.posts.select("posts.*, count(posts.user_id) as user_posts_count").group('posts.user_id')
.order("user_posts_count DESC").limit(10)
但是我的postgreSQL问题,而我的代码在sqlit3和mysql2上没有任何问题
PG :: GroupingError:错误:列“posts.id”必须出现在GROUP BY子句中或用于聚合函数 第1行:SELECT posts。*,count(posts.user_id)as user_posts_count FR ...
我需要使用postgreSQL,因为它最适合heroku 但我不知道为什么我在PostgreSQL上出现这个错误
我希望有人帮助我>>>
答案 0 :(得分:0)
从组中删除id,你应该在group by中添加所有其他字段,使用。*是一个不好的实践,请不要使用。* in select,例如,count()