The following article describes how to insert data from an actual application table to another table in the MySQL database. This solution looks simple but not so, although this implementation is very simple, it will still plague many new users. Therefore, I will issue an article for future reference.
During development, we often need to insert data from a table to another table. Sometimes we need to specify the import field. Although this implementation is very simple, it will still plague many new users, therefore, an article is specially published for future reference.
If the fields of the two tables are consistent and you want to insert all the data, you can use this method:
Code:
Insert into target table SELECT * FROM source table;
For example, to insert the articles table to the newArticles table:
Code:
Insert into newArticles SELECT * FROM articles;
If you only want to import a specified field, you can use this method:
Code:
Insert into target table (Field 1, Field 2,...) SELECT Field 1, Field 2,... FROM source table;
Note that the order of fields must be consistent.
If you want to import only records that do not exist in the target table, refer to another article.
Insert if not exists when the record does not exist in the MySQL database)
The above content is an introduction to how to insert data from one table into another table in the MySQL database.
Edit recommendations]