mysql升級後出現Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated colum...

安裝了mysql5.7,用group by 查詢時拋出以下異常:php

Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.t_long.user_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

緣由:

      MySQL 5.7.5和up實現了對功能依賴的檢測。若是啓用了only_full_group_by SQL模式(在默認狀況下是這樣),那麼MySQL就會拒絕選擇列表、條件或順序列表引用的查詢,這些查詢將引用組中未命名的非聚合列,而不是在功能上依賴於它們。(在5.7.5以前,MySQL沒有檢測到功能依賴項,only_full_group_by在默認狀況下是不啓用的。關於前5.7.5行爲的描述,請參閱MySQL 5.6參考手冊。)css

執行如下個命令,能夠查看 sql_mode 的內容。html

mysql> SHOW SESSION VARIABLES;
mysql> SHOW GLOBAL VARIABLES;
mysql> select @@sql_mode;

可見session和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_SUBSTITUTIONjava

only_full_group_by說明: 
only_full_group_by :使用這個就是使用和oracle同樣的group 規則, select的列都要在group中,或者自己是聚合列(SUM,AVG,MAX,MIN) 才行,其實這個配置目前我的感受和distinct差很少的,因此去掉就好 
官網摘抄: 
官網:ONLY_FULL_GROUP_BY 
Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.mysql

As of MySQL 5.7.5, the default SQL mode includes ONLY_FULL_GROUP_BY. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default. For a description of pre-5.7.5 behavior, see the MySQL 5.6 Reference Manual.)sql

A MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list. Before MySQL 5.7.5, enabling ONLY_FULL_GROUP_BY disables this extension, thus requiring the HAVING clause to be written using unaliased expressions. As of MySQL 5.7.5, this restriction is lifted so that the HAVING clause can refer to aliases regardless of whether ONLY_FULL_GROUP_BY is enabled.shell

解決:

執行如下兩個命令:數據庫

mysql> 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';
mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

這兩個命令,去掉 sql_mode 的 ONLY_FULL_GROUP_BYexpress

見其餘文章有說: 
直接修改mysql配置文件(個人系統是Ubuntu16.04的,在/etc/mysql/mysql.conf.d/mysqld.cnf 中並無sql_mode這個配置,因此直接加上就好,若是是其餘系統有得修改就不用添加了) 
這個方法暫時沒有式。windows

mysql 配置信息讀取順序。

①ps aux|grep mysql|grep ‘my.cnf’

②mysql –help|grep ‘my.cnf’

/etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf 這些就是mysql默認會搜尋my.cnf的目錄,順序排前的優先。mysql按照上面的順序加載配置文件,後面的配置項會覆蓋前面的。

若是沒有該文件能夠自定義一個文件。而後回默認讀取配置中的內容) 
查看你須要修改的是哪一個配置文件。我只有/etc/my.cnf 只修改這個文件便可

配置文件my.cnf一般會分紅好幾部分,如[client],[mysqld], [mysql]等等。MySQL程序一般是讀取與它同名的分段部分,例如服務器mysqld一般讀取[mysqld]分段下的相關配置項。若是配置項位置不正確,該配置是不會生效的

參考:https://stackoverflow.com/questions/37951742/1055-expression-of-select-list-is-not-in-group-by-clause-and-contains-nonaggr

這個語句沒試過,先記錄:

set @@sql_mode='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便可正常執行sql.

補充說明

一、 以上sql執行修改以後要先斷開sql的session鏈接才能生效,也就是說若是是在生產中,要將項目重啓後才能正常。
二、windows上msyql服務從新開啓後,會將sql的設置清除,須要去配置文件中配置,個人參考配置以下:

[mysql]

#設置mysql客戶端默認字符集

default-character-set=utf8mb4 [mysqld] #配置sqlmode便面系統升級致使的問題 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' #設置3306端口 port = 3306 #設置mysql的安裝目錄 basedir=E:\DevTools\mysql-5.7.25-winx64 #設置mysql數據庫的數據的存放目錄 datadir=E:\DevTools\mysql-5.7.25-winx64\data #容許最大鏈接數 max_connections=200 #服務端使用的字符集默認爲8比特編碼的latin1字符集 character-set-server=utf8 #建立新表時將使用的默認存儲引擎 explicit_defaults_for_timestamp=true default-storage-engine=INNODB 
相關文章
相關標籤/搜索