首页 > 代码库 > 50个查询系列-第13个查询:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

50个查询系列-第13个查询:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

UPDATE tblscore SET tblscore.Score=

(


-- 这里开始算叶平的平均值
SELECT AVG(tt.aa) FROM 
(
SELECT  tblscore.Score aa FROM tblscore WHERE
tblscore.CourseId=
( 

SELECT tblcourse.CourseId FROM tblcourse WHERE tblcourse.CourseId=(
SELECT tblteacher.TeaId FROM tblteacher WHERE tblteacher.TeaName=叶平 )
))tt -- 算出叶平教得课程的平均值


 
 
 
 )
 WHERE tblscore.CourseId=
 
 (SELECT tblcourse.CourseId FROM tblcourse WHERE tblcourse.CourseId=(
SELECT tblteacher.TeaId FROM tblteacher WHERE tblteacher.TeaName=叶平 ))-- 叶平上课的id

 

 

 

 

 

 

答案的写法:

 

Select * Into Sc From tblScore
 go
 Update Sc Set Score=(Select Avg(Score) From tblScore s1 Where s1.CourseId=sc.CourseId) 
  Where CourseId IN
   (Select CourseId From tblCourse cs INNER JOIN tblTeacher tc ON cs.TeaID=tc.TeaID WHERE TeaName =叶平)

 

50个查询系列-第13个查询:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;