Selectintooutfile: SQL statement for exporting data

Source: Internet
Author: User
The selectintooutfile command is a common command used in mysql to export data. next I will introduce the usage of selectintooutfile. if you need it, refer to it.

The select into outfile command is a common command used in mysql to export data. next I will introduce the usage of select into outfile. if you need it, refer to it.

Mysql can easily export data by managing data. However, when the data volume is large, phpmyadmin may encounter execution timeout. In fact, it is easy to use select into outfile!

The main function of the SELECT... into outfile statement is to allow you to quickly dump a table to a server machine. If you want to create a result file on a client host other than the server host, you cannot use SELECT... into outfile. In this case, you should use the command "mysql-e" SELECT... "> file_name" on the client host to generate a file.

SELECT... into outfile is the complement of load data infile. the syntax used for the exort_options part of the statement includes some FIELDS and LINES clauses, which are used together with the load data infile statement


As follows: (export the wp_posts table in the database wordpress to disk D)

The code is as follows:

Select * into outfile 'd: data.txt 'from wp_posts

Note: to use the select into outfile statement, you must have the execution permission and the path must exist. Otherwise, you must use the current database directory,

Example 2

The code is as follows:

Table structure and test data
Create table 'tab '(

'Id' INT (1) not null AUTO_INCREMENT primary key,

'Name' VARCHAR (10) not null,

'Add' VARCHAR (20) character set gbk NOT NULL

) ENGINE = InnoDB default charset = utf8;

Insert into 'tab' ('name', 'ADD') VALUES

('Chinese', 'This column won't be garbled '),

('Chinese characters', 'this column will not be garbled ');

SELECT * FROM 'tab ';

+ ---- + ------ + -------------- +

| Id | name | add |

+ ---- + ------ + -------------- +

| 1 | Chinese | this column is not garbled |

| 2 | Chinese characters | this column is not garbled |

+ ---- + ------ + -------------- +
Rows in set (0.00 sec)

Code

The code is as follows:

Use variables in INTO OUTFILE
SET @ tmp_ SQL = CONCAT (
"SELECT * into outfile 'C: \ SQL _", DATE_FORMAT (NOW (), '% Y % m % d % H % I % S '),
". Txt 'Fields TERMINATED by', 'optionally enclosed by" '"lines terminated by 'RN' FROM 'tab ';");
PREPARE s1 FROM @ tmp_ SQL;
EXECUTE s1;
Drop prepare s1;

The specified code will generate the “SQL _current time .txt" file under the C drive to avoid the INTO OUTFILE file error.

Note: No garbled characters are displayed in the SELECT command line directly, but garbled characters exist after being exported using into outfile. in this case, you must modify the character set of the columns with garbled characters.

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.