原文链接:何晓东 博客

在 MySQL 5.7版本以上进行一些 ORDER BY 或者 GROUP BY 时,会呈现如下谬误 [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 的束缚是:你进行了 GROUP BYORDER BY,就须要保障你 SELECT 的列都在 GROUP BYORDER BY 中,然而理论需要并没有这样简略。

 长期批改 ONLY_FULL_GROUP_BY 模式

查问进去现有的模式

select @@global.sql_mode;

去掉外面的 ONLY_FULL_GROUP_BY 模式,而后set 回去

set @@global.sql_mode = "查出来的值,去掉 ONLY_FULL_GROUP_BY"
 批改配置文件中的 ONLY_FULL_GROUP_BY 模式

找到 my.ini 文件,批改其中的 sql_mode

sql_mode="查出来的值,去掉 ONLY_FULL_GROUP_BY"
 借助 and_value() 函数疏忽没有参加分组的列

能够应用 ANY_VALUE() 援用未聚合的列,而无需禁用 ONLY_FULL_GROUP_BY 即可取得雷同的成果。

例如:

mysql> SELECT name, MAX(age) FROM t;ERROR 1140 (42000): In aggregated query without GROUP BY, expression#1 of SELECT list contains nonaggregated column 'mydb.t.name'; thisis incompatible with sql_mode=only_full_group_by

借助 ANY_VALUE() 改成这样就好了:

SELECT ANY_VALUE(name), MAX(age) FROM t;

参考链接: MySQL Handling of GROUP BY

最初恰饭 阿里云全系列产品/短信包特惠购买 中小企业上云最佳抉择 阿里云外部优惠券