解决Group By语句报错:this is incompatible with sql_mode=only_full_group_by

今天在我的个人服务器上的Mysql中写了这么一条SQL语句:

select id,name,link,passwd,original,type from movie GROUP BY link;

执行竟然报错,报错信息如下:

[2018-07-06 14:53:27.339] [000172] [个人线上数据库] [MYSQL]
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.22,是我最近做个人项目时新装的一个,这条SQL放在我公司服务器mysql(版本5.6)中执行是没毛病的,因为group by语句的用法是:group by后的字段要出现在select后面或者使用聚合函数即可,上官网查了下对于sql_mode=only_full_group_by的解释如下:

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.

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

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.

也就是说,在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的,only_full_group_by就表示你group by的字段必须和select后的字段完全匹配才行(聚合函数除外)。

查下我的mysql中sql_mode属性值:

show variables like '%sql_mode%'

如下命令重新设置下sql_mode属性值:

 set session sql_mode='';

我这里是直接设置为空了,具体的sql_mode取值可以参考官网:mysql之sql_mode属性及取值

重新执行SQL,OK!

本文参考文献:mysql官网

猜你喜欢

转载自blog.csdn.net/fanrenxiang/article/details/80941895
今日推荐