怎么批量更新?
两个表t1,t2,要求用t2的值来更新t1的值,即t1=sum(t2) 的值
如
t1:
id number
101 10
102 5
t2:
id quantity
101 51
101 100
101 80
102 50
102 90
最后的结果应该是
t1:
id numbwe
101 241
102 145
[解决办法]
;
WITH t2
AS ( SELECT id ,
SUM(quantity) quantity
FROM t2
GROUP BY id
)
UPDATE t1
SET t1.number = t2.quantity
FROM t1
INNER JOIN t2 ON t1.id = t2.ID
--2000的写法
UPDATE t1
SET t1.number = t2.quantity
FROM t1
INNER JOIN ( SELECT id ,
SUM(quantity) quantity
FROM t2
GROUP BY id
) t2 ON t1.id = t2.ID