MySQL,复制被选定字段从一个表到另一个(MYSQL, Copy selected fields

2019-06-25 17:11发布

在MySQL中,如何复制一个字段与所有记录TABLE1TABLE2对应一个主键,即: EMPLOYEE no.

Answer 1:

试试这个

INSERT INTO `table2` (`field_name2`) SELECT `field_name` FROM `table1`


Answer 2:

如果你的意思是你想用另一个表的列更新一个表的列,那么这里有一些选择:

  1. 一个连接:

     UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo SET t1.SomeColumn = t2.SomeColumn 

    或者它可能是一个左连接:

     UPDATE table1 AS t1 LEFT JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo SET t1.SomeColumn = t2.SomeColumn 

    这将基本上是空的(设置为NULL)不匹配发生时的值。

  2. 子查询:

     UPDATE table1 SET SomeColumn = ( SELECT SomeColumn FROM table2 WHERE EmployeeNo = table1.EmployeeNo ) 

    这是在#1等同于左连接解决方​​案。

请注意,在所有情况下,假设在一排table1可以匹配不超过一个排table2



Answer 3:

为将数据复制到另一个查询从一个表:

Insert into table2 (field1, field2)  select field1, field2 from table1


如果你只想复制选定的值,然后用在查询的WHERE子句

Insert into table2 (field1, field2)  select field1, field2 from table1 where field1=condition




Answer 4:

update
  table1 t1
  join table2 t2 on t2.field = t1.field
set
  t1.field1 = t2.matchingfield
where
  t1.whatever = t2.whatever


Answer 5:

你可以用这个来的所有记录从复制table1table2与条件。

Insert into table2  select  * from table1 where field1=condition


Answer 6:

INSERT INTO table_1column-1column-2 )SELECT柱1,柱2 FROM TABLE_2;



Answer 7:

Insert into Delivery (DeliveredDate, appid, DownloadSize, UploadSize) select Delivered, Appid, DownloadSize,UploadSize from Delivery_Summary;



文章来源: MYSQL, Copy selected fields from one table to another