gpt4 book ai didi

sql - 根据另一个表中的值更新 2 列

转载 作者:行者123 更新时间:2023-11-29 13:52:00 25 4
gpt4 key购买 nike

我有 2 个表,我想写 2 个更新。
tbl_stage with id, flag (null) and count(null) column.
带有 ID、计数、更新列的 tbl_source。

更新前

tbl_stage                 tbl_source
id flag count id count updated
abc NULL NULL abc 9.0 false
def 3.6 false

我想更新 tbl_stage.count=tbl_source.count 和 tbl_stage.flag = true 其中 tbl_stage.id=tbl_source.id。
另一个,tbl_source.updated = true 其中 tbl_stage.id=tbl_source.id。

更新后应该是这样的。

tbl_stage                 tbl_source
id flag count id count updated
abc true 9.0 abc 9.0 true
def 3.6 false

最佳答案

您肯定需要使用 2 个 UPDATE 语句,因为在一个语句中更新 2 个表是不可用的。

update tbl_stage
set [count]=tbl_source.count
,flag = 1
FROM tbl_source WHERE tbl_stage.id=tbl_source.id

update tbl_source
set updated = 1
from tbl_source WHERE tbl_stage.id=tbl_source.id
and tbl_stage.count=tbl_source.count

关于sql - 根据另一个表中的值更新 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295548/

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