使用mysql5.7 执行以下代码时,系统会报错
SELECT r.sub_name,AVG(score) AS 平均分,MAX(score),MIN(score)
FROM result r
INNER JOIN `subject` sub
ON r.sub_id = sub.sub_id
GROUP BY sub.sub_id
报错信息为
错误代码: 1055
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'school.r.sub_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因是因为在mysql5.7之前的版本,
MySQL没有检测到功能依赖项,
only_full_group_by在默认情况下是不启用的。
在mysql5.7中only_full_group_by 默认是启用的。
所以MySQL会拒绝选择列表、条件或顺序列表引用的查询,
这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。
解决办法
1.打开Mysql的配置文件,找到 **.ini 文件并打开,然后在[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
2.打开doc界面,然后关闭mysql服务,然后重启,使上面的命令生效,重启后即可正常执行刚才报错的语句