日期:2014-05-18  浏览次数:20552 次

100分求一SQL语句!
问题描述:
表A(id_a,   x)
数据:1,   0

表B(id_b,   y)
数据:1,   2
数据:1,   4
数据:1,   9
           

现要做类似如下更新:UPDATE   A   set   x   =   x   +   sum(y)   from   B   where   id_a=id_b

当然,以上sql语句是错误的,执行后表A的数据应为:1,15

这个sql语句应该怎么写?

------解决方案--------------------
UPDATE TA
SET x = x + TB.y
FROM TA
INNER JOIN (SELECT id_b, SUM(y) AS y FROM B GROUP BY id_b)TB
ON TA.id_a = TB.id_b
------解决方案--------------------

UPDATE A set x = x + b.y from A,
(Select id_b,sum(y) as y from B group by id_b) as b where a.id_a=b.id_b
------解决方案--------------------
update A set x=x+y from (select id_b,sum(y) as y from B group by id_b) B where id=B.id_b
------解决方案--------------------
update A set x=x+B.y from (select id_b,sum(y) as y from B group by id_b) B where id=B.id_b

------解决方案--------------------
UPDATE A set x = x + (select isnull(sum(y),0) from B where id_a=A.id_b)