MySql - insert data from one table to another

10 points
Created by:

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`);


INSERT INTO `dst_table` (SELECT * FROM `src_database`.`src_table`);

Note: access to `src_database` is necessary.

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé