Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'img_id'
安装了mysql5.7,用group by 查询时抛出如下异常:
Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'cslgdata.img_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因:
MySQL 5.7.5和up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。
解决方法:
1.登录MySQL
mysql -u root -p
2. 输入SHOW SESSION VARIABLES;
SHOW GLOBAL VARIABLES;
select @@sql_mode;
这时候你会看到session和global 的sql_mode的值都是一样的:
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
执行以下两个命令:
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';
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';
这两个命令,去掉 sql_mode 的 ONLY_FULL_GROUP_BY