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

女生的网名这么多〃 提交于 2020-03-18 10:55:53

问题


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:

Error: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #8 of SELECT list
is not in GROUP BY clause and contains nonaggregated column 'wis.Tags.id' which is not
functionally dependent on columns in GROUP BY clause; this is incompatible with 
sql_mode=only_full_group_by

Furthermore, i get the information to check my "BookmarksTags" table but i do not have to creat one before. I little bit confused.

Please try correcting the issue for the following table aliases:

BookmarksTags

I already google my problem and i found information to update the "my.cnf" with a extra line. i already try but nothing happed. I also check the spelling and downloaded the "bookmarker-tutorial" from github but i still get this error above.

I use MySQL 5.7.11 and PHP 5.6.14.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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'])


来源:https://stackoverflow.com/questions/36228836/syntax-error-or-access-violation-1055-expression-8-of-select-list-is-not-in-gr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!