欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySql 5.7+ ONLY_FULL_GROUP_BY 引发问题解决方法

程序员文章站 2023-12-28 15:21:40
...
一般都是使用group by 引发的异常

-Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘###’…
this is incompatible with sql_mode=only_full_group_by

原因:mysql 5.7+ 版本only_full_group_by SQL 默认开启

其实就是使用了oracle的group规则啦,导致旧版本写的sql无法通过

解决方法:
mysql> SHOW SESSION VARIABLES;
mysql> SHOW GLOBAL VARIABLES;
mysql> select @@sql_mode;

得到sql-mode例如:

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

注意:不要复制,个人数据库配置不同,将所查的sql_mode去除ONLY_FULL_GROUP_BY后重新set

set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

ps: 直接设置sql_mode为修改全局变量,只对新建数据库有效,已有数据库需进入后执行上方set代码

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

上一篇:

下一篇: