在 Microsoft SQL Server 中:
这个问题我已经看过好几次了,但是看不到有人解决这个问题:
UPDATE theTable t1
SET t1.col2 =
(SELECT (10 * (col1 + col2))
FROM theTable t2
WHERE t1.busRelAccount = t2.busRelAccount
AND t2.rowName = 'POS'
)
WHERE t1.busRelAccount = t2.busRelAccount
AND t1.rowName = 'INVENTORY'
;
有人在不使用 CURSOR 和循环表格的情况下解决了这个问题吗?
最佳答案
不能 100% 确定您要做什么 - 但最有可能的是,您可以编写更简单的 UPDATE
语句 - 像这样:
UPDATE t1
SET t1.col2 = 10 * (t2.col1 + t2.col2)
FROM theTable t1
INNER JOIN theTable t2 ON t1.busRelAccount = t2.busRelAccount
WHERE
t1.rowName = 'INVENTORY'
AND t2.rowName = 'POS'
;
基本上,您在两个表(实际上是同一个表)之间定义一个 JOIN,然后定义如何根据 t2中的列更新
t1
的列
关于sql-server - 基于选择到同一个表的更新 - AGAIN 和 AGAIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009563/