日期:2014-05-17  浏览次数:20744 次

2个表,update求汇总
比我有2个表 a,b

a是这样的

kpbh totalsum
0001
0002
0003
0004

b是这样的

kpbh totalsum
0001 1
0001 2
0002 3
0002 4
0003 5
0003 6
0004 7
0004 8
0005 9

现在我想更新表a 的totalsum 等于 b相同kpbh 的求和
我的语句应该怎么写?

------解决方案--------------------
探讨
SQL code


--1
update a
set totalsum =(select sum(totalsum ) from b where b.kpbh = a.kpbh)
where exists(select 1 from a,b where a.kpbh=b.kpbh);

--2
merge into a
using (select kpbh,……