SQL 更新 - 多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15881487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Update - Multiple Columns
提问by Prakash Chennupati
I would like to update multiple columns in a table based on values from a second table using a Select
statement to obtain the values like this:
我想根据第二个表中的值更新表中的多个列,使用Select
语句获取如下值:
UPDATE tbl1
SET (col1, col2, col3) = (SELECT colA, colB, colC
FROM tbl2
WHERE tbl2.id = 'someid')
WHERE tbl1.id = 'differentid'
However, it doesn't seem as though it's possible to 'SET' more than one column name - are there alternatives rather than writing separate update statements for each column?
但是,似乎不可能“设置”多个列名称 - 是否有替代方法而不是为每一列编写单独的更新语句?
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'
回答by Tevo D
update tbl1
set col1 = a.col1, col2 = a.col2, col3 = a.col3
from tbl2 a
where tbl1.Id = 'someid'
and a.Id = 'differentid'
回答by Arun
This should work -
这应该有效 -
Update Tbl1
SET
Col1 = B.ColA,
Col2 = B.ColB,
Col3 = B.ColC
FROM
Tbl2 B
Where
B.Id = 'Someid'