UPDATE + SELECT 更新问题
A表
A1,A2, A3
1 A 0
2 B 0
B表
B1,B2,B3
1 A 10
2 B 2
3 B 1
如何写一个UPDATE 的语句把B表的B3列的数字累加到A表的A3
A1,A2, A3
1 A 10
2 B 3
[解决办法]
update a set a.A3=b.B3 from A表 a join B表 b on a.A1=b.B1-- 是用的a.A1=b.B1做为条件,看楼主是不是要这个条件。
[解决办法]
update a set a.A3=b.B3 from A表 a join (select B2,sum(B3) from b group by b2) b on a.A2=b.B2
[解决办法]
create table a(a1 int,a2 varchar(10),a3 int)insert into a select 1,'a',0 union allselect 2,'b',0create table b(b1 int,b2 varchar(10),b3 int)insert into b select 1,'a',10 union allselect 2,'b',2 union allselect 3,'b',1update a set a.A3=(select sum(b.b3) from b where b.b2=a.a2)--drop table a--drop table b------------------------1 a 102 b 3