简体   繁体   English

如何将一列从一个表复制到另一个表

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

I got two tables. 我有两张桌子。 I need to copy data from one table to another based on id 我需要根据ID将数据从一个表复制到另一个表

table1 表格1

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

table 2: 表2:

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

How can i copy the year column from table 1 to table two so that they have correct ids 如何将年份列从表1复制到表2,以便它们具有正确的ID

This will update the year column in Table2 to match the value in Table1 where the id is the same in both tables: 这将更新表2中的year列以匹配表1中两个表中的id相同的值:

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

Sample SQL Fiddle 示例SQL提琴

For reference: MySQL manual for UPDATE 供参考: MySQL UPDATE手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM