only_full_group_by 是什么鬼 - mysql - 海诺者 - 海诺博客公告板

起源

昨天装了听云apm 插件,于是一晚上的报警不断,发现总是提示 mysql4200错误

    1. ["42000",1055,"Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'xxx.x2_examhistory.ehusername' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"]. sql=[SELECT count(*) AS number,ehusername,max(ehscore) as ehscore FROM x?_examhistory WHERE ehstarttime >= :ehstarttime GROUP BY ehuserid ORDER BY number DESC LIMIT ??]
  • 查找原因

    度娘帮了忙,发现这样子的事情 sql_mode 中默认是 only_full_group_by 这是什么鬼
    通过 mysql 官方13.20.3 MySQL Handling of GROUP BY 这里可以详细的值,在使用了主键获取数据的情况下如果开启了 only_full_group_by (msyql 5.7 默认开启,5.6及之前不开启),那么如果 select fields 中的字段有计算就会被拒绝。主要原因如果使用了 id,且 id 为主键那么 你所用的 max(age) 中的 age 是由固定值的从而就出现这样的问题,max(age)和 age 是不匹配的 max(age)要求一个列表,而没有被提供。所以拒绝。

    解决方式

    只需要将 sql_mode 中的值only_full_group_by 去掉即可。

        原文作者:MySql
        原文地址: https://juejin.im/entry/5850e3f58e450a006c807f13
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞