SELECT 
A.class_id.
B.subject,
AVG(B.socre) AS average_score 
From A join B on A.student_id = B.student_id
group by A.class_id, B.subject;

假设新的表结构如下:

要计算每个班级每个科目的平均成绩,SQL查询可以这样写:

SELECT
    A.class_id,
    B.subject,
    AVG(B.score) AS average_score
FROM
    A
JOIN
    B
ON
    A.student_id = B.student_id
GROUP BY
    A.class_id,
    B.subject;

解释:

  1. JOIN:通过A.student_id = B.student_id将表A和表B进行关联,获取每个学生的班级、科目和成绩。
  2. GROUP BY:按A.class_idB.subject分组,以便对每个班级的每个科目进行聚合计算。
  3. AVG(B.score) AS average_score:计算每个班级每个科目的学生成绩的平均值,并将其命名为average_score

这个查询将返回每个班级每个科目的平均成绩。