相关推荐recommended
MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理
作者:mmseoamin日期:2024-01-25

目录

  • 一、异常错误
  • 二、原因
  • 三、解决方法

    一、异常错误

    使用联表查询时,group by两个字段出现了错误

    MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理,在这里插入图片描述,第1张

    MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理,在这里插入图片描述,第2张

    Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'train_c.e.ques_type' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
    

    意思是select字段里包含了没有被group by 条件唯一确定的字段。

    二、原因

    MySQL版本5.7之后会检测函数依赖,默认启用的模式是ONLY_FULL_GROUP_BY,使用GROUP BY 语句违背了sql_mode=only_full_group_by。该模式的意思是只有确定唯一字段的group by才能执行。

    使用SQL查询sql_mode

    select @@global.sql_mode
    

    MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理,在这里插入图片描述,第3张

    三、解决方法

    在MySQL-server层面修改sql_mode,在配置文件my.cnf中关闭sql_mode=ONLY_FULL_GROUP_BY

    ,将sql_mode配置放在 [mysqld] 后

    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    

    MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理,在这里插入图片描述,第4张

    重启mysqld服务

    systemctl restart mysqld.service
    systemctl start mysqld.service
    systemctl stop mysqld.service
    

    成功

    MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理,在这里插入图片描述,第5张