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

前端 未结 5 819
时光说笑
时光说笑 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:53

    I'm using Laravel 5.4 and facing same problem.
    I try to set strict into false on config/database.php and it works.

    'connections' => [
        'mysql' => [
            'strict' => false,
        ]
    ]
    

    However it's better to edit sql query than suppress it's warning.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-13 16:04

    Just stumbled on the same problem.
    A simple way to make the tutorial "work" without changing the query could be to reset sql_mode in config/app.php :

    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            ...
            'init' => ["SET sql_mode = ''"],
        ],
    

    While this should makes things work, it is probably not recommended in real life cases.

    0 讨论(0)
  • 2021-02-13 16:10

    This may be the issue related to newer version of MySQL as you said you are using MySQL 5.7.11 So to solve this issue for all your sites see this

    https://craftcms.stackexchange.com/questions/12084/getting-this-sql-error-group-by-incompatible-with-sql-mode-only-full-group-by/13662#13662

    the answer by community to add the line below

    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 
    

    in /etc/mysql/my.cnf file & saving it; and after restarting the server

    sudo service mysql restart
    

    Would do the work

    0 讨论(0)
  • 2021-02-13 16:13

    In addition to tadman's comment, for tutorial you are following, you should distinct your sql select query by both Bookmarks.id and BookmarksTags.tag_id, instead of just Bookmarks.id

    To do that, in BookmarksTable.php file, line

    ->distinct(['Bookmarks.id'])
    

    should look like

    ->distinct(['Bookmarks.id', 'BookmarksTags.tag_id'])
    
    0 讨论(0)
提交回复
热议问题