Student(S#,Sname,Sage,Ssex) 学生表 S#:学号;Sname:学生姓名;Sage:学生年龄;Ssex:学生性别
Course(C#,Cname,T#) 课程表 C#,课程编号;Cname:课程名字;T#:教师编号
SC(S#,C#,score) 成绩表 S#:学号;C#,课程编号;score:成绩
Teacher(T#,Tname) 教师表 T#:教师编号; Tname:教师名字
问题:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩:
网上的答案根本不靠谱,而且这里我的叶平教了2门课
------解决思路----------------------
最近刚学习到 merge这个东西
merge into sc
using (select sc.c# C#, avg(sc.score) SCORE
from sc
where sc.c# in ('002', '005')
group by sc.c#) sc2
on (sc.c# = sc2.c#)
when matched then
update set sc.score = sc2.score;
------解决思路----------------------
UPDATE sc a
SET (a.score) =
(SELECT AVG(b.score)
FROM sc b
WHERE a.c# = b.c#)
WHERE EXISTS (SELECT 1
FROM sc b,
course c,
teacher d
WHERE a.s# = b.s#
AND a.c# = b.c#
AND c.c# = b.c#
AND c.t# = d.t#
AND d.tname = '叶平')
------解决思路----------------------
merge into SC U
using ( select A.C#, avg(A.Score) as Score
from SC A,
Course B,
Teacher C
where B.C# = A.C#
and C.T# = B.T#
and C.Tname = '叶平'
group by A.C# ) V
on ( V.C# = U.C# )
when matched then
update set U.Score = V.Score;