通过一个子查询得到学生的SNo,SName,平均成绩,将这个结果插入到表score,这个sql语句怎么写??
求下面的SQL语句
要求:通过一个子查询得到学生的学号、姓名、平均成绩,将这个结果插入到表score,其中平均成绩是要算出来的,以下是有关联的表
表SC
SNoCNoSDateScore
1033-2452009-05-0186
1053-2452009-06-1175
1093-2452010-06-2368
1033-1052008-10-1192
1053-1052010-10-1488
1093-1052010-10-1576
1013-1052009-10-2064
1073-1052009-10-1788
1083-1052010-10-1878
1016-1662008-05-1885
1076-1662008-06-2179
1086-1662009-06-0881
Student
SNoSNameSexBirthDateClassEmail
108曾华男1982-09-0195033zengh@sohu.com
105匡明男1982-10-0295031kuangm@sina.com.cn
107王丽女1981-01-2395033 wangl@hotmail.com
101李军男1983-02-2095033lij@163.com
109王芳女1982-02-1095031wangf@yahoo.com
103陆君男1980-06-0395031luj@263.com
[解决办法]
- SQL code
select a.sno,a.sname,avg(b.score) score into #tbfrom student a join SC b on a.sno = b.sno--where ...group by a.sno,a.snameinsert into score(sno,sname,score)select *from #tb twhere not exists (select 1 from score where sno = t.sno)update aset a.score = b.scorefrom score a join #tb b on a.sno = b.snodrop table #tb
[解决办法]
- SQL code
insert into score(sno,sname,score)select sno,sname,(select avg(score) from sc where sc.SNo=student.SNo)from student