Use of the insert into select statement in the MySQL database

Source: Internet
Author: User

This article mainly introducesMySQL databaseStatement: the use of insert into select and Its syntax. Next we will introduce it through an example. We hope that this example will help you better understand the syntax of this statement, avoid unnecessary errors in future use. Now let's get started.

1. Syntax Introduction

There are three tables a, B, and c. Now we need to query the values of several fields from Table B and Table c and insert them to the corresponding fields in table. In this case, you can use the following statement:

 
 
  1. INSERT INTO db1_name (field1,field2)  
  2.  
  3. SELECT field1,field2 FROM db2_name 

The preceding statement is suitable for data insertion between two tables. For multiple tables, you can first JOIN the fields to be queried, and then form a view, and then select from:

 
 
  1. INSERT INTO a (field1,field2)   
  2.  
  3. SELECT * FROM(SELECT b.f1,c.f2 FROM b JOIN c) AS tb 

F1 is the field of Table B, f2 is the field of table c, and fields from Table B and Table c are combined through JOIN query, and then SELECTNested QueryInsert to Table a to meet this scenario. If more than two tables are required, you can combine fields in the form of multiple joins.

2. syntax errors

Note that the table alias must be set at the end of the nested query section, as shown below:

 
 
  1. SELECT * FROM (SELECT f1,f2 FROM b JOIN c) AS tb 

That is, the name of the final AS tb is a required tb. An alias must be specified for each derived new table. Otherwise, the following error is reported in mysql:

 
 
  1. ERROR 1248 (42000): Every derived TABLE must have its own alias 

In addition, insert into select in MySQL cannot be added with VALUES, that is, it cannot be written as follows:

 
 
  1. INSERT INTO db1_name(field1,field2) VALUES SELECT field1,field2 FROM db2_name 

Otherwise, an error is reported:

 
 
  1. You have an error in your SQL syntax 

The syntax of the MySQL database is introduced here. If you want to learn more about the MySQL database, you can refer to the article here: http://database.51cto.com/mysql/, which will bring you a good harvest!

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.