把“SC ”表中“葉平”老師教的課的成績都更改為此課程的平均成績;


--學生表tblStudent(編號StuId、姓名Stuname、年齡Stuage、性別Stusex)
--課程表tblCourse(課程編號CourseId、課程名稱CourseName、教師編號TeaId)
--成績表tblScore(學生編號StuId、課程編號CourseId、成績Score)
--教師表tblTeacher(教師編號TeaId、姓名TeaName)
--13、把“SC ”表中“葉平”老師教的課的成績都更改為此課程的平均成績; (從子查詢中獲取父查詢中的表名,這樣也行????)
--查詢教師編號
select * from tblTeacher te where te.teaname='葉平'
--查詢‘葉平’教的課程
select * from tblCourse co where co.teaid='002'
--查詢‘葉平’教的課程的成績表
select * from tblScore sc where sc.courseid in (001,016)
--查詢平均值
select sc.courseid,count(*),avg(sc.score) from tblScore sc where sc.courseid in (
select co.courseid from tblCourse co where co.teaid in(
select te.teaid from tblTeacher te where te.teaname='葉平'
)
) group by sc.courseid
--修改平均值
update tblScore sc set sc.score=(
--這里從子查詢中獲取父查詢中的表名 使平均值的課程等於父類條件的課程
select avg(score) from tblscore where courseid=sc.courseid
)
where sc.courseid in (
select co.courseid from tblCourse co where co.teaid in(
select te.teaid from tblTeacher te where te.teaname='葉平'
)
);
--再次查詢‘葉平’教的課程的成績表看看是否修改成功
select * from tblScore sc where sc.courseid in (001,016)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM