在mysql中使用 group by 出现的问题
java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'wxcall.wx_free_chat_record.user_token' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
问题原因:
only_full_group_by 的意思是:对于 group by 聚合操作,如果在select中的列,没有在 group by 中出现,那么这个sql是不合法的,因为列不在 group by 从句中,也就是说查出来的列必须在group by后面出现,否则就会报错,或者这个字段出现在聚合函数里面。
快速解决方法:
没有在 group by 中出现的列, 放进 ANY_VALUE() 中
例如:
SELECT
COUNT(1) AS everydayUserCount,
date_format(a.create_time, '%Y-%m-%d') AS days,
ANY_VALUE(b.channelName) AS channelName
FROM
wx_user a
LEFT JOIN wx_channel b ON b.id = a.wx_channel_id
GROUP BY
days