2013-12-19 65 views
1

所以我有這兩個表。更新表與ID匹配的另一個表的數據

table1 
+----+---------+ 
| id | type_id | 
+----+---------+ 
| 1 |  1 | 
+----+---------+ 
| 2 |  12 | 
+----+---------+ 

table2 
+----+-----------+---------+ 
| id | table1_id | type_id | 
+----+-----------+---------+ 
| 5 |   1 |  0 | 
+----+-----------+---------+ 
| 6 |   2 |  0 | 
+----+-----------+---------+ 

我想用table1.type_id中的值更新table2.type_id,使用table1中的id作爲參考點。

我無法將我的大腦圍繞如何做到這一點。

回答

3
UPDATE table2 
SET type_id = a.type_id 
FROM table2 b 
    JOIN table1 a ON a.id = b.table_id 

本聲明將充分利用其在table2JOIN數據得當,從tablea獲得的價值。

1
UPDATE T2 
SET table1_id = T1.type_id 
FROM table2 AS T2 
JOIN table1 AS T1 
    ON T1.id = T2.table1_id 
相關問題