升級MySQL到5.7後,查詢語句老是報sql_mode=only_full_group_by問題,總結概括了兩種解決方案,推存第二種解決方案。mysql
報錯信息:
[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_bysql
問題緣由:數據庫
mysql 5.7版本默認sql_mode配置是包括:sql_mode="ONLY_FULL_GROUP_BY",這個配置嚴格執行了"SQL92標準"。當從5.6升級到5.7時,爲了語法兼容,大部分都會選擇調整sql_mode,使其保持跟5.6一致,爲了儘可能兼容程序提示了報錯信息。vim
解決方案:this
1. sql語句暫時性修改sql_mode,重啓mysql數據庫服務以後,ONLY_FULL_GROUP_BY報錯還會出現。spa
查看sql_mode
select @@global.sql_mode;rest
查出結果爲:
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_SUBSTITUTIONorm
去掉ONLY_FULL_GROUP_BY,從新設置值:cmd
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';it
2. 修改MySQL配置文件,完全解決問題。
需修改mysql配置文件,經過手動添加sql_mode的方式強制指定不須要ONLY_FULL_GROUP_BY屬性,
my.cnf位於etc文件夾下,vim下光標移到最後添加以下(Windows是mysql安裝目錄下面的 my.ini 配置文件):
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
重啓MySQL服務,完全解決報錯。
重啓命令:
Linux: service mysqld restart /etc/init.d/mysqld restart
Windows命令cmd: net stop mysql net start mysql