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

mysql5.7 用group by出现如下报错

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'xxxxx' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决办法如下:

方法一

1、执行如下语句查看sql_model

SHOW SESSION VARIABLES;

在这里插入图片描述

SHOW GLOBAL VARIABLES;

在这里插入图片描述

2、可见session和global的sql_model都为

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_SUBSTITUTION

only_full_group_by :这个和oracle的group是一样的规则,select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行
建议去掉only_full_group_by就好

3、解决办法
在这里插入图片描述

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'

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'

注意:记得先执行session,再执行global,我试过先执行global,还是会报错

方法二

见好多博客中写,但我执行后依然报错,建议使用方法一,亲测可行!

select @@global.sql_mode
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';

你可能感兴趣的:(sql)