2015-12-10 46 views
1

更新tableB中的id列,其中id爲tableA,其中引用是縣。如果這些國家匹配,那麼這個ID應該填入tableB通過參照另一個表更新一個表的列的SQL查詢

請參考屏幕截圖以供參考。

表B: enter image description here

表A: enter image description here

我嘗試了查詢,但未能解決這個問題。

**update tableB set Id from tableB,tableA where tableA.countries=tableB.country;** 

**update tableB set Id=(select id from tableA where countries=(select countries from tableA))** 
+0

其DBú[R使用? – Sachu

+0

sql server manageemnt studio 2012 – anu

+1

我的上帝,請在提交之前使用拼寫檢查器。 –

回答

0

ü可以嘗試此查詢

UPDATE 
    tableB 
SET 
    tableB.ID = A.ID 
FROM 
    tableB B 
INNER JOIN 
    tableA A 
ON 
    B.country = A.countries 
+0

@anu你可以試試這個代碼 – Sachu

+0

感謝它的工作。 – anu

+0

歡迎兄弟 – Sachu

相關問題