1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘se

 Navicat Premium 16 版本

这个错误是由于 MySQL 的新版本中默认开启了ONLY_FULL_GROUP_BY模式,即在 GROUP BY 语句中的 SELECT 列表中,只能包含分组或聚合函数,不能包含其他列。而你的查询语句中出现了一个列senior_two.score.student_id,它既没有被分组也没有被聚合,因此 MySQL 报出了这个错误。

原句:

SELECT * FROM score LEFT JOIN course on score.course_id=course.course_id GROUP BY course.course_name

运行后报错:

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'senior_two.score.student_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决这个问题的方法有两种:

  1. 关闭ONLY_FULL_GROUP_BY模式。在:mysql> 界面中输入下面的命令即可关闭:
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','');

        2 .将senior_two.score.student_id列加入 GROUP BY 列表中,这样 MySQL 就能知道如何处理这个列了。例如,修改你的查询语句为:

SELECT senior_two.score.student_id, AVG(senior_two.score.score) AS avg_score FROM senior_two.score GROUP BY senior_two.score.student_id;

这个查询将按学生 ID 分组,并计算每个学生的平均分。这样 MySQL 就不会报出上述错误了。

扫描二维码关注公众号,回复: 15890463 查看本文章

猜你喜欢

转载自blog.csdn.net/m0_61615803/article/details/130076452