Mysql问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause

1 问题描述

使用Navicat连接到MySQL(版本:8.0.18),执行查询:

select * from t_user WHERE  user_name = 'admin'

查询结果没有问题,但是报错:

[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问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause_第1张图片

2 原因

MySql从5.7版本及以上你版本默认开启only_full_group_by规则。

only_full_group_by规则:
1) order by后面的列必须是在select后面存在的。
2) select、having或order by后面存在的非聚合列必须全部出现在group by子句中。

3 解决方案

3.1 遵循only_full_group_by规则;

如:

select t.column1, t.column2 from table as t group by column1

修改:

select t.column1, t.column2 from table as t group by column1,column2 

3.2 去掉only_full_group_by规则

第1步:查询当前的规则,执行查询:

SELECT @@sql_mode;

执行结果:
Mysql问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause_第2张图片

第2步:去掉only_full_group_by规则,执行查询:

set @@GLOBAL.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

执行结果:
Mysql问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause_第3张图片
第3步:再次查询当前的规则,执行查询:

SELECT @@sql_mode;

执行结果:
Mysql问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause_第4张图片
说明only_full_group_by规则已去掉。

4 验证

去掉only_full_group_by规则后,执行查询:

select * from t_user WHERE  user_name = 'admin'

执行结果:
Mysql问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause_第5张图片
没有报错,问题解决。

你可能感兴趣的:(#,Mysql,mysql,数据库,sql)