2014-10-27 98 views
0

我有两张桌子。我需要将数据从一个表中根据ID如何从一张桌子复制一列到另一张

table1的

+----+------------------------+------+ 
| id | title     | year | 
+----+------------------------+------+ 
| 1 | Carmencita    | 1894 | 
| 2 | Le clown et ses chiens | 1892 | 
| 3 | Pauvre Pierrot   | 1892 | 
+----+------------------------+------+ 

表复制到另一个2:

+----+------------------------+------+ 
| id | title     | year | 
+----+------------------------+------+ 
| 1 | Carmencita    | 0 | 
| 2 | Le clown et ses chiens | 0 | 
| 3 | Pauvre Pierrot   | 0 | 
+----+------------------------+------+ 

如何从表1中复制当年列于表中的两个,这样他们有正确的ID

回答

3

这将更新表2中年的列以匹配Table1中的值,其中ID是两个表中是相同的:

update Table2 
inner join Table1 on Table1.`id` = Table2.`id` 
set Table2.`year` = table1.`year`; 

Sample SQL Fiddle

参考:MySQL manual for UPDATE

相关问题