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.
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:
The Code is as follows:
Insert into target table SELECT * FROM source table;
For example, to insert the articles table to the newArticles table:
The Code is as follows:
Insert into newArticles SELECT * FROM articles;
If you only want to import a specified field, you can use this method:
The Code is as follows:
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 (insert if not exists)