1. 程式人生 > >mysql查詢報錯: ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

mysql查詢報錯: ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

在用mysql執行如下查詢的時候:

select * from `sys_user_group` group by `GROUP_ID`

報錯資訊如下:

[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的,使用如下語句查詢可知

select @@sql_mode;

這裡寫圖片描述

裡面預設設定了

sql_mode=only_full_group_by

only_full_group_by :使用這個就是使用和oracle一樣的group 規則, select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行,其實這個配置目前個人感覺和distinct差不多的,所以去掉就好

直接修改mysql配置檔案(我的系統是Ubuntu16.04的,在/etc/mysql/mysql.conf.d/mysqld.cnf

中並沒有sql_mode這個配置,所以直接加上就好,如果是其他系統有得修改就不用添加了)

[mysqld]下

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配置檔案的完整配置如下:

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
# user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking lower_case_table_names=1 character-set-server=utf8 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION # # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1 # # * Fine Tuning # key_buffer_size = 16M max_allowed_packet = 16M thread_stack = 192K