1. 程式人生 > >MySQL 5.5.X版本GROUP BY錯誤解決方法

MySQL 5.5.X版本GROUP BY錯誤解決方法

安裝新本版的MySQL GROUP BY分組查詢會出現錯誤提示如下:

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

MySQL 5.7.X的預設的sql_model值為(進入MySQL輸入命令select @@global.sql_mode即可檢視):

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

問題就出現在ONLY_FULL_GROUP_BY上,需要把該值去掉即可.
以Linux的MySQL 5.7.20為例說明修改方法:
開啟mysql配置檔案sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf,然後在[mysqld]下新增如下一行:

sql_mode=STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER

預設配置中沒有sql_mode,自行新增此行,添加了此行配置就把ONLY_FULL_GROUP_BY預設的去掉了,儲存之後重啟MySQL即可(sudo service mysql restart).

提示:如果進入MySQL命令列方式設定sql_model值,重啟Mysql就又恢復預設值了,屬於當前session範圍的.所以最好直接修改配置檔案,這樣就會永久有效.