我有两张桌子
T1
|id|balance|
|1| 1000 |
|2| 2000 |
|3| 3000 |
T2
|id|rate|months|
|1|0.50| 10 |
|2|0.25| 24 |
|3|0.40| 16 |
我想将表T1中的余额列更新为
T1.Balance = T1.Balance + T2.rate*T2.months*T1.Balance
如何在MS SQL服务器中执行此操作?谢谢!
答案 0 :(得分:0)
以下查询可以帮助您使用单个查询更新特定列的所有行。
UPDATE T1 AS t1 SET t1.balance = t1.balance * (SELECT t2.rate * t2.months FROM T2 AS t2 WHERE t2.id = t1.id);
或者你也可以尝试这个
UPDATE T1 AS t SET t.balance = (t1.balance * t2.rate * t2.months) FROM T1 AS t1 JOIN T2 AS t2 ON t1.id = t2.id;