簡體   English   中英

如何將一列從一個表復制到另一個表

[英]How can I copy a column from one table to another

我有兩張桌子。 我需要根據ID將數據從一個表復制到另一個表

表格1

+----+------------------------+------+
| 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復制到表2,以便它們具有正確的ID

這將更新表2中的year列以匹配表1中兩個表中的id相同的值:

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

示例SQL提琴

供參考: MySQL UPDATE手冊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM