Generate MySQL scripts in batches in Excel)

Source: Internet
Author: User

1. Assume that your table contains columns A, B, and C and you want to import the data to the table in your database. The corresponding fields are col1, col2, and col3.

 

2. Add a column to your table and use the Excel Formula to automatically generate an SQL statement. The specific method is as follows:

 

1. Add a column (assuming column D)

 

2. In column D of the first row, enter the formula in D1:
= Concatenate ("insert into table (col1, col2, col3) values ('", A1, "', '", B1, "', '", C1, "');")

 

3. At this time, D1 has generated the following SQL statement:
Insert into table (col1, col2, col3) values ('A', '11', '33 ');

 

4. Copy the D1 formula to column D of all rows (that is, use the mouse to hold the lower right corner of the D1 cell and drag it down)

 

5. All SQL statements have been generated in column D.

 

6th, copy the "D" column to a pure text file, and set it to "SQL .txt ".

 

You can run SQL .txt in the database by using the command line or phpadmin.

 

4. For the import of large data volumes, it is easy to create a script for database flushing according to the above method. However, importing only one record at a time is costly and time-consuming. Therefore, you can modify the import formula to import batch data into the database at one time to save the database flushing time and system resources.

 

Assume that there are a total of 1000 records, as shown in the following figure:

 

The first (1st rows, including the first data ):

= Concatenate ("insert into table (col1, col2, col3) values ('", A1, "', '", B1, "', '", C1, "'),")

First (1st rows, excluding data)

= Concatenate ("insert into table (col1, col2, col3) values ")

 

Central region (2nd ~ 999 rows ):

= Concatenate ("('", A2, "', '", B2, "', '", C2 ,"'),")

 

Last entry (1,000th rows ):

= Concatenate ("('", a1000, "', '", B1000, "', '", c1000 ,"');")

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.