标签:ssi mode data ble new code sql错误 without only
In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column ‘ynsl_new.u.user_id‘; this is incompatible with sql_mode=only_full_group_by
1、原sql语句:
SET @data_id=‘153f61e9727b450d8abbba5941e5085a‘; SELECT u.*, GROUP_CONCAT(r.ROLE_CODE SEPARATOR ‘,‘) AS roleCode FROM tab_user u LEFT JOIN tab_user_role_relation ur ON ur.user_id = u.user_id LEFT JOIN tab_role r ON r.role_id = ur.role_id WHERE u.delete_mark = 0 AND u.enable_mark = 1 AND u.data_id = @data_id
2、中文大概意思:
在不带GROUP BY的聚合查询中,选择列表的表达式1包含未聚合的列“ynsl_new.u.user_id”;这与SQL_mode=only_full_group_by不兼容
也就是说,mysql的sql_mode是only_full_group_by的时候,在不使用group by 并且select后面出现聚集函数的话,那么所有被select的都应该是聚集函数,否则就会报错;
3、出现的原因:
在MySQL5.7.5后,默认开启了ONLY_FULL_GROUP_BY,所以导致了之前的一些SQL无法正常执行,其实,是我们的SQL不规范造成的,因为group by 之后,返回的一些数据是不确定的,所以才会出现这个错误。
1、关闭ONLY_FULL_GROUP_BY
在配置文件中,找到sql_mode,去掉only_full_group_by
2、规范sql语句,使用group by
SET @data_id=‘153f61e9727b450d8abbba5941e5085a‘; SELECT u.*, GROUP_CONCAT(r.ROLE_CODE SEPARATOR ‘,‘) AS roleCode FROM tab_user u LEFT JOIN tab_user_role_relation ur ON ur.user_id = u.user_id LEFT JOIN tab_role r ON r.role_id = ur.role_id WHERE u.delete_mark = 0 AND u.enable_mark = 1 AND u.data_id = @data_id GROUP BY u.user_id ;
基于【 bug解决】一 || mysql的ONLY_FULL_GROUP_BY导致的sql语句错误
标签:ssi mode data ble new code sql错误 without only
原文地址:https://www.cnblogs.com/kevin-ying/p/11601809.html