Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.

安装了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参考手册。)html

执行如下个命令,能够查看 sql_mode 的内容。java

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_SUBSTITUTIONmysql

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.web

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.数据库

解决:

执行如下两个命令:express

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_BYwindows

见其余文章有说:
直接修改mysql配置文件(个人系统是Ubuntu16.04的,在/etc/mysql/mysql.conf.d/mysqld.cnf 中并无sql_mode这个配置,因此直接加上就好,若是是其余系统有得修改就不用添加了)
这个方法暂时没有式。bash

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的设置清除,须要去配置文件中配置,个人参考配置以下: ```java

[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

相关文章
相关标签/搜索