EN
MySql - insert data from one table to another
10
points
Using MySQL it is possible to insert data from one table to another in following way.
1. INSERT ... SELECT
query example
This examples show how to copy data from `src_table`
to `dst_table`
.
1.1. Copping selected colums
Only selected columns are copied with created one value ('copied'
).
INSERT INTO `dst_table` (`name`, `status`, `source_id`)
SELECT `name`, 'copied', `id`
FROM `src_table`
ORDER BY `name` ASC
1.2. Copping all columns
To use this approach both tables should have similar coumns.
INSERT INTO `dst_table` (SELECT * FROM `src_table`);
or
INSERT INTO `dst_table` (SELECT * FROM `src_database`.`src_table`);
Note: access to
`src_database`
is necessary.