簡體   English   中英

更新引用另一個表

[英]Update referencing another table

我有一條語句需要寫(用通用名稱表示,因為這是為了工作)來更新表“ tUpd”中的列“ updCol”。 tUpd的另一個表tOther中也有一個'linkCol'列。 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 a.col = b.col來加入where子句。 可以說這很尷尬,但是允許靈活地進行日志記錄,而我對此並不完全了解。 所以,我正在尋找非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)

編輯: 另一個問題的多列解決方案

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)

也許會對您有幫助

update tUpd
set tU.updCol = XXX
from tOther tot, tUpd tU   
where tot.linkCol = tU.linkCol
  and tot.idCol = MY_ID

是類似問題的鏈接。

就像這樣

UPDATE DestinationTable
SET DestinationTable.UpdateColumn =  SourceTable.UpdateColumn 
FROM SourceTable
WHERE DestinationTable.JoinColumn = SourceTable.JoinColumn

這適用於Informix數據庫:

UPDATE dest_table V 
SET field_1 = 
   (SELECT field_1 FROM source_tbl WHERE field_2 IS NULL
   AND field_1 = V.field_1);

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM