2013-12-19 66 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 
相关问题