我想使用Select
语句根据第二个表中的值更新表中的多个列,以获取如下值:
UPDATE tbl1
SET (col1, col2, col3) = (SELECT colA, colB, colC
FROM tbl2
WHERE tbl2.id = 'someid')
WHERE tbl1.id = 'differentid'
然而,看起来似乎并不可能设置'多个列名称 - 是否有替代方案而不是为每列编写单独的更新语句?
UPDATE tbl1
SET col1 = (SELECT colA FROM tbl2 WHERE tbl2.id = 'someid')
WHERE tbl1.id = 'differentid'
UPDATE tbl1
SET col2 = (SELECT colB FROM tbl2 WHERE tbl2.id = 'someid')
WHERE tbl1.id = 'differentid'
UPDATE tbl1
SET col3 = (SELECT colC FROM tbl2 WHERE tbl2.id = 'someid')
WHERE tbl1.id = 'differentid'
答案 0 :(得分:32)
update tbl1
set col1 = a.col1, col2 = a.col2, col3 = a.col3
from tbl2 a
where tbl1.Id = 'someid'
and a.Id = 'differentid'
答案 1 :(得分:7)
这应该有效 -
Update Tbl1
SET
Col1 = B.ColA,
Col2 = B.ColB,
Col3 = B.ColC
FROM
Tbl2 B
Where
B.Id = 'Someid'
答案 2 :(得分:-1)
您可以找到答案here。