gpt4 book ai didi

sql - 更新引用另一个表

转载 作者:行者123 更新时间:2023-12-05 08:28:27 27 4
gpt4 key购买 nike

我有一个语句需要编写(使用通用名称,因为这是为了工作)以更新表“tUpd”中的列“updCol”。 tUpd 也有一个“linkCol”列,它存在于另一个表 tOther 中。 tOther 有另一列“idCol”。

我的问题是更新 tUpd 中行的 updCol 值,这些行通过 linkCol 对应于具有给定 idCol 值的行。

我认为应该可行的一个解决方案如下;

update
tUpd
set
updCol = XXX
where exists (
select
idCol
from
tOther
where
tOther.linkCol = tUpd.linkCol
and tOther.idCol = MY_ID
)

但是,我担心这种方法会导致性能不佳,因为我之前被警告过与性能相关的子查询——这个子查询将针对 tUpd 的每一行运行一次,这是正确的吗?

有没有人有更好的建议?

重要更新:我的工作场所不惜一切代价避免使用 SQL JOIN,而更喜欢使用例如 where a.col = b.col 在 where 子句中加入。这可以说是相当尴尬,但允许灵 active ,特别是我不完全理解的日志记录。所以,我正在寻找不使用 JOIN 的解决方案 :)

最佳答案

所有上述解决方案都会在 Informix 中给出一个错误,因为它无法找到表中的一个。这是一个对我有用的解决方案:

update table1
set table1.field2 = (select table2.field2 from table2 where table1.field1 = table2.field1)
where table1.field1 in (select table2.field1 from table2)

编辑:来自 another question 的多列解决方案

update table1
set (table1.field2, table2.field3) = (
(select table2.field2, table2.field3
from table2
where table1.field1 = table2.field1)
)
where table1.field1 in (select table2.field1 from table2)

关于sql - 更新引用另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493575/

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