The use of the INSERT into select from MySQL

Source: Internet
Author: User

How to combine fields from multiple tables in MySQL and then insert them into a new table, implemented by an SQL statement. The case is: there are three tables A,B,C, now you need to check the values of several fields from table B and table C into the corresponding fields in table A. In this case, we can use the following statement to implement:

1 INSERT  into SELECT  from Db2_name

Of course, the above statement is more suitable for two tables of data interpolation, if more than one table is not adapted. For multiple tables, we can first join the fields that need to be queried , then compose a view and Select from:

1 INSERT  into SELECT *  from (SELECTfromJOIN as TB

Where F1 is the field of table B,F2 is the field of Table C, and the join query combines the fields from table B and Table C, and then inserts them into the table by a select nested query A, this satisfies our scenario, if more than 2 tables are needed, you can combine the fields in multiple join forms. It is important to note that the nested query section must finally have a set table alias, as follows:

1 SELECT *  from (SELECTfromJOIN as TB

That is, the last as TB is a must (of course , the name of TB can be arbitrary), that is, specify an alias, or in MySQL will report the following error:

1 1248 (42000TABLE must has its own alias

That is, each new table you derive must specify an alias.

Ext.: http://blog.csdn.net/fuxuejun/article/details/6277169

The use of the INSERT into select from MySQL

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.