2017-10-05 59 views
1

替換值的值我有兩個表:SQL Server的更新,並從另一個表

連接

id user_id connection_info 
------------------------------ 
1 1  ... 
2 1  ... 
3 2  ... 

列表

id connection_id name 
----------------------- 
1  1   ... 
2  2   ... 
3  1   ... 

我現在有user_id「 s在lists.connection_id列。我想加入lists表,connections表由connections.user_id = lists.connection_id,然後用connections表中的相應ID替換lists.connection_id

+2

所以你嘗試一些東西,失敗?如果是的話請分享你的代碼/錯誤。如果沒有,嘗試一個解決方案,並分享給人們一些工作。 – Tanner

回答

1

您可以使用UPDATE FROM這樣的:

update l 
set l.connection_id = c.id 
from connections c join lists l on c.user_id = l.connection_id 

最初你想測試你要更新的內容,運行SELECT聲明:

select l.connection_id as con_old 
    , c.id as con_new 
    , ... (other cols you might want to check) 
from connections c join lists l on c.user_id = l.connection_id 
相關問題