sql - 更新引用另一个表

我有一个语句需要编写(使用通用名称,因为这是为了工作)以更新表“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)

https://stackoverflow.com/questions/12493575/

相关文章:

ruby-on-rails - 仅显示 f.label 覆盖 f.input 标签

linq-to-sql - 从 Telerik 扩展网格获取分页、过滤并传递给存储过程

encryption - 解码 AES-256 key 需要多少种组合?

ruby-on-rails - Rails, Controller 中条件查询之间的简单数字

vba - Access 自定义组

unix - 用另一个值替换 unix 文件中的最后一列

php - 从php中的日期检索日期,月份,年份

php - 如何从 CSV 文件中删除重复行?

sql-server - 普通的 SQL Server 索引是否包含主键值?

mongodb - 为什么叫NoSQL?