簡體   English   中英

使用子查詢和連接進行更新

[英]Update with a subquery and a join

如何使用此查詢的結果更新table1 -

select b.description from 
table1 c
join 
(
    SELECT distinct a.hcpc, a.description
    FROM table2 a 
) b 
on b.hcpc = c.hcpc
where c.description = '0'

我想做這樣的事情-

update table1 
set table1.description = (select b.description from table1 c
join 
(
    SELECT distinct a.hcpc, a.description
    FROM table2 a 
) b 
on b.hcpc = c.hcpc
where c.description = '0'
) 

這個問題已經在SO中回答了很多次......無論如何,查詢應該以這種方式構建

一種選擇

update table1 c
set description = b.description
from
  (
    SELECT distinct a.hcpc, a.description
    FROM table2 a 
  ) b  
where b.hcpc = c.hcpc
  and c.description = '0'

其他選項

with t as (
  SELECT distinct a.hcpc, a.description
  FROM table2 a
)
update table1 c
set description = t.description
from t
where t.hcpc = c.hcpc
  and c.description = '0'

當然還有其他的方法...

暫無
暫無

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

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