MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理

目录

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

一、异常错误

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

在这里插入图片描述

在这里插入图片描述

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-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

在这里插入图片描述

重启mysqld服务

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

成功

在这里插入图片描述

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/c9be7e7e3b.html