[求sql语句]统计一个表,用统计结果批量更新另外一个表。

fihuang 2012-03-13 11:21:55
学生表Students,学生选修课程的成绩表CourseScore。
现在需要跟据CourseScore里的数据进行统计以后更新Students表的TotalScore字段(即根据学生选修课程的成绩更新学生的总分)。
为了方便大家,我把sql语句写出来,如下所示:

create table Students
(
StudentID int identity(1,1) primary key,
Name nvarchar(32) not null,
TotalScore int not null
);
insert into Students(Name, TotalScore) values('刘德华', 0);
insert into Students(Name, TotalScore) values('张学友', 0);
insert into Students(Name, TotalScore) values('郭富城', 0);


create table CourseScore
(
StudentID int not null,
CourseID int not null,
Score int not null
);
insert into CourseScore values(1 , 1, 80);
insert into CourseScore values(1 , 2, 80);
insert into CourseScore values(1 , 3, 80);
insert into CourseScore values(2 , 2, 95);
insert into CourseScore values(2 , 3, 85);
insert into CourseScore values(3 , 1, 100);
insert into CourseScore values(3 , 2, 75);


谢谢大家!
...全文
261 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2012-03-14
  • 打赏
  • 举报
回复
create table Students
(
StudentID int identity(1,1) primary key,
Name nvarchar(32) not null,
TotalScore int not null
);
insert into Students(Name, TotalScore) values('刘德华', 0);
insert into Students(Name, TotalScore) values('张学友', 0);
insert into Students(Name, TotalScore) values('郭富城', 0);

create table CourseScore
(
StudentID int not null,
CourseID int not null,
Score int not null
);
insert into CourseScore values(1 , 1, 80);
insert into CourseScore values(1 , 2, 80);
insert into CourseScore values(1 , 3, 80);
insert into CourseScore values(2 , 2, 95);
insert into CourseScore values(2 , 3, 85);
insert into CourseScore values(3 , 1, 100);
insert into CourseScore values(3 , 2, 75);
go

update Students set TotalScore = isnull((select sum(Score) from CourseScore where StudentID = t.StudentID),0) from Students t

select * from Students
/*

StudentID Name TotalScore
----------- -------------------------------- -----------
1 刘德华 240
2 张学友 180
3 郭富城 175

(所影响的行数为 3 行)
*/

drop table Students, CourseScore
dawugui 2012-03-14
  • 打赏
  • 举报
回复
update Students set TotalScore = isnull((select sum(Score) from CourseScore where StudentID = t.StudentID),0) from Students t
flyfly2008 2012-03-14
  • 打赏
  • 举报
回复
这样方法更新值得学习
fihuang 2012-03-14
  • 打赏
  • 举报
回复
看了大家的回复,非常感谢。有个问题请教一下,为何你们的sql语句最后都要加“from ...”

我这样的写法和你们有啥区别不?
update Students
set TotalScore = (select SUM(Score) from CourseScore where StudentID = Students.StudentID);
叶子 2012-03-13
  • 打赏
  • 举报
回复
--简化一下
--更新
update a set TotalScore=(select sum(Score) from CourseScore where StudentID=a.StudentID)
from Students a
百年树人 2012-03-13
  • 打赏
  • 举报
回复
update a set a.TotalScore=b.s
from Students a
join (select StudentID,sum(Score) s from CourseScore group by StudentID) b
on a.StudentID=b.StudentID

select * from Students
/**
StudentID Name TotalScore
----------- -------------------------------- -----------
1 刘德华 240
2 张学友 180
3 郭富城 175

(3 行受影响)
**/
叶子 2012-03-13
  • 打赏
  • 举报
回复

--更新
update Students set TotalScore=b.c1
from Students a left join
(
select StudentID,sum(Score) as c1
from CourseScore group by StudentID
) b
on a.StudentID=b.StudentID

--查看
select * from Students
/*
StudentID Name TotalScore
----------- -------------------------------- -----------
1 刘德华 240
2 张学友 180
3 郭富城 175
*/

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧