gpt4 book ai didi

SQL 更新 - 多列

转载 作者:行者123 更新时间:2023-12-03 11:36:38 39 4
gpt4 key购买 nike

我想使用 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'

最佳答案

update tbl1
set col1 = a.col1, col2 = a.col2, col3 = a.col3
from tbl2 a
where tbl1.Id = 'someid'
and a.Id = 'differentid'

关于SQL 更新 - 多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15881487/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com