How to Insert into a table values from another Select From Table

Eduardo | 2018-05-11

How to Insert into a table values from another Select From Table
This is a simple tutorial on how to Insert values into a table that comes from another table. (MySQL)

Many times we need to transfer data from one table to another, or even duplicate content on the same Database table. 

The easiest way to do this, is by specifying the columns that we want to insert content to, and select the data with the same number of columns from another table, or even itself:

 

INSERT INTO table_name1
      (col1, col2, col3, coL4) 
SELECT colA, colB, colC, colD FROM table_name2 WHERE condition;  

 

The select query can be combined with joins or any other kind of query structure, as long as the resulting query contains the same number of values to be inserted on the destination table.

Comments