出错代码:
SELECT c.course_no, c.course_name, c.school_year, c.semester, c.credit,
max(sc.score) AS max_score, min(sc.score) AS min_score, avg(sc.score) As avg_score
FROM select_course sc, course c
WHERE sc.course_no = c.course_no
GROUP BY c.course_no
原因
1:只要有聚合函数 sum(),count(),max(),avg() 等函数就需要用到 group by , 否则就会报上面的错误.
2:group by id (id 是主键) 的时候, select 什么都没有问题, 包括有聚合函数.
3:group by role (非主键) 的时候, select 只能是聚合函数和 role ( group by 的字段) , 否则报错
更正:将course表中另外两个主键都添加进去即可运行
SELECT c.course_no, c.course_name, c.school_year, c.semester, c.credit,
max(sc.score) AS max_score, min(sc.score) AS min_score, avg(sc.score) As avg_score
FROM select_course sc, course c
WHERE sc.course_no = c.course_no
GROUP BY c.course_no, c.school_year, c.semester