SELECT list is not in GROUP BY clause and contains nonaggregated column 't_user_rel.distance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=on
错误信息
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column
't_user_rel.distance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SQLSTATE[42000]: Syntax error or access violation: 1140 In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column
't_team.uid'; this is incompatible with sql_mode=only_full_group_by
发生错误的源sql语句
SELECT `uid`,`distance` FROM `t_user_rel` WHERE `uid` IN (7534,2967,2145,901,746,654,2) GROUP BY `uid` ORDER BY distance asc"
其中distance 不是group by的字段 ,数据库做了限制 sql_mode = only_full_group_by,所以报错
解决方法
错误信息就是sql语句使用group by的时候 获取字段column 不是分组的字段,mysql配置做了限制: sql_mode=only_full_group_by
网上查了很多的解决方法就是修改配置文件,就可以解决这个问题。
参考:https://blog.csdn.net/qq_42175986/article/details/82384160
解决方法就是把
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
设置成
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
就是把分组查询字段不做限制
查看数据库sql_mode的配置
SHOW VARIABLES LIKE '%sql_mode%'
yum指定官方源安装好的不同mysql版本的默认sql_mode 的配置如下:
MySQL5.7.24
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
MySQL5.7.25
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
MySQL5.7.26
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
由上可以得出5.7.26以及之后的版本ONLY_FULL_GROUP_BY 是官方默认的配置