MYSQL, Copy selected fields from one table to another
update
table1 t1
join table2 t2 on t2.field = t1.field
set
t1.field1 = t2.matchingfield
where
t1.whatever = t2.whatever
If you mean you want to update one table's column using another table's column, then here are some options:
A join:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo SET t1.SomeColumn = t2.SomeColumn
Alternatively it could be a left join:
UPDATE table1 AS t1 LEFT JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo SET t1.SomeColumn = t2.SomeColumn
which would essentially empty (set to NULL) the values where no match occurred.
A subquery:
UPDATE table1 SET SomeColumn = ( SELECT SomeColumn FROM table2 WHERE EmployeeNo = table1.EmployeeNo )
This is equivalent to the left join solution in #1.
Note that in all cases it is assumed that a row in table1
can match no more than one row in table2
.
Try this
INSERT INTO `table2` (`field_name2`) SELECT `field_name` FROM `table1`
The query for copy data from one table to another is:
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
If you want to copy only selected values, then use where clause in query
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
WHERE `field1` = condition