Languages
[Edit]
EN

MySql - insert data from one table to another

13 points
Created by:
Argon
617

In this article, we would like to show how to insert data from one table to another in MySQL.

Quick solution:

INSERT INTO `dst_table` (SELECT * FROM `src_table`)

Where:

  • src_table means source table name,
  • dst_table means destination table name.

 

Practical examples

In this section, we want to show how to copy data with INSERT ... SELECT query.

1.1. Copping selected columns

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 columns.

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

or

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

Note: use src_database only if source table is located in different database.

Alternative titles

  1. MySql - copy data from one table to another
  2. MySql - copy data from one table to second one
  3. MySql - clone data between tables
  4. MySql - rewrite data from one table to second one (insert from select)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - Problems

MySql - insert data from one table to another
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.

❤️💻 🙂

Join