Insert... select in mysql to copy table data

Source: Internet
Author: User

In mysql, data in the complex tabulation can be implemented simply by using insert... select. Next I will give you an example of simple table data replication.

Syntax

Insert into db1_name (field1, field2) SELECT field1, field2 FROM db2_name

Instance

You can run the insert... select syntax to solve the problem:

Insert into 1__ktv (title, price, number, date, area, content, num) select title, price, number, date, area, content, num from 1__ktv;


View results

The Code is as follows: Copy code
Mysql> select * from hotel_ktv;
+ ---- + ------- + -------- + ------------------- + ------ + -------------------- + ----- +
| Id | title | price | number | date | area | content | num |
+ ---- + ------- + -------- + ------------------- + ------ + -------------------- + ----- +
| 1 | K1 | 1680 | 20 |-the next day | 70 | remarks: 15% service charge is required. | 1 |
| 2 | K1 | 1680 | 20 |-the next day | 70 | Note: 15% service charge is required. | 1 |
| 3 | K1 | 1680 | 20 |-the next day | 70 | remarks: 15% service charge is required. | 1 |
| 4 | K1 | 1680 | 20 |-the next day | 70 | remarks: 15% service charge is required. | 1 |
+ ---- + ------- + -------- + ------------------- + ------ + -------------------- + ----- +
4 rows in set (0.00 sec)

Four identical data records have been successfully copied to facilitate data testing.

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:

The Code is as follows: Copy code

Insert into a (field1, field2) 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 queries, then insert the data to Table a through the SELECT nested query. This satisfies this scenario. If more than two tables are required, fields can be combined 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:

The Code is as follows: Copy code

SELECT * FROM (SELECT f1, f2 FROM B JOIN c) AS tb

That is, the final AS tb is required (the tb name can be retrieved at will), that is, an alias is specified. An alias must be specified for each derived new table. Otherwise, the following error is reported in mysql:

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:

The Code is as follows: Copy code

Insert into db1_name (field1, field2) values select field1, field2 FROM db2_name
 

Otherwise, the following error occurs: You have an error in your SQL syntax.

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.