MySQL database inserts data from one table into another table

Source: Internet
Author: User

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]

 

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.