2014-10-27 133 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

相關問題