Syntax error or access violation: 1055 Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column

前端 未结 5 829
时光说笑
时光说笑 2021-02-13 15:28

i tried the CakePHP 3.x \"Bookmaker Tutorial\" and i followed the instruction step by step. Unfortunately, at the end of the first chapter i get the attached error:



        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 15:56

    This is a new thing in MySQL 5.7 and is a warning that your query is ambiguous.

    Consider the following table:

    id    |   name    |   age    |   points
    --------------------------------------------
    1         Bob         21         1
    2         James       14         1
    3         Bob         21         3
    4         James       14         2
    5         Casey       17         3
    

    If you did the following query:

    SELECT name, age, SUM(points) FROM scores GROUP BY name
    

    Then the name column is used for grouping. Note that age may have multiple values, so it's "non-aggregated". You need to do something to collapse down those values.

    The behaviour in 5.6 and previous was to just pick the first one depending on sort order, though this was sometimes unpredictable and would fail. In 5.7 they're preventing you from doing it in the first place.

    The solution here is to group on that as well, or to apply an aggregate operator like MIN() to it.

提交回复
热议问题