Implementation statement for inserting data from one table into another table in MySQL

Source: Internet
Author: User
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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.