Languages
[Edit]
EN

MySql - insert data from one table to another

10 points
Created by:
JustMike
3485

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.

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍