GroupingError: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function

前端 未结 2 1739
情话喂你
情话喂你 2020-11-29 04:15

I have code in my controller that is ranking albums by the highest average review rating (used code from this solution How to display highest rated albums through a has_many

2条回答
  •  爱一瞬间的悲伤
    2020-11-29 04:39

    You are not allowed to select reviews.id (selected implicitly through the wildcard *) without adding it to the GROUP BY clause or applying an aggregate function like avg(). The solution is to do one of the following:

    1. Remove the wildcard * from your select
    2. Add the field reviews.id to your group clause
    3. Select reviews.id explicitly and apply an aggregate function to it (e.g. sum(reviews.id))
    4. Replace the wildcard * with the table-specific wildcard albums.*

    The second and third option do not make much sense in your scenario though. Based on your comment, I added option four.

提交回复
热议问题