An implementation statement that inserts data from one table into another table under MySQL

  • 2020-05-09 19:27:21
  • OfStack

If field 1 of two tables is identical and you want to insert all the data, you can use this method:
Code:
 
INSERT INTO  The target table  SELECT * FROM  The source table ; 

For example, to insert the articles table into the newArticles table, it is:
 
INSERT INTO newArticles SELECT * FROM articles; 

If you only want to import the specified fields, you can use this method:
 
INSERT INTO  The target table  ( field 1,  field 2, ...) SELECT  field 1,  field 2, ... FROM  The source table ; 

Note that the order of the fields must be 1 to 1.
If you need to import only records that do not exist in the target table, refer to another article
MySQL insert when the record does not exist (insert if not exists)

Related articles: