将一个表的查询更新到另一个表,求一SQL语句!
A表
id | name | sum
1 a 0
2 b 0
3 c 0
B表
id | name | number
1 a 5
2 b 3
2 b 3
1 a 6
3 c 5
1 a 1
3 c 5
先统计B表的数据
select id,name,sum(number) from [B] group by id
现在要将这个查询的记录集更新到A表中
这个SQL该怎么写?
------解决方案--------------------update A set A.id=B.id,A.name=B.name,A.[sum]=B.[sum] from
(
select id,name,[sum]=sum(number) from [B] group by id
)
where A.id=B.id and A.name=B.name
------解决方案--------------------update A set [sum]=T.hj
from A
inner join (select id,sum(number) as hj from B group by id) AS T on A.id=T.id