SQL statement for exporting data from mysql to an excel file

Source: Internet
Author: User
To export data in mysql, you can use the export outfile command to quickly export data records to xls files. For more information, see.

To export data in mysql, you can use the into outfile command to quickly export data records to xls files. For more information, see.

The code is as follows:

Mysql> select * from xi_table into outfile 'd: test.xls ';

Export as a txt file:

The code is as follows:

Select * from xi_table into outfile 'd: test.txt ';


By default, fields are separated by tab, and records are separated by n. You can customize three parameters.

The code is as follows:

SELECT * FROMhx_9enjoy into outfile '/tmp/9enjoy_hx.txt 'Fields TERMINATED by', 'optionallyenclosed BY' "'lines terminated by 'N ';

It is used to separate fields. double quotation marks are used to limit the content of fields. n is used to separate records.

You can also specify the record output for certain conditions:

The code is as follows:

SELECT * FROMhx_9enjoy WHERE id <100 into outfile '/tmp/9enjoy_100.txt'

If the excel file contains garbled characters when it is opened, we can set the encoding to gb2312 during export and use iconv on the server side for encoding conversion:

The code is as follows:

Iconv-futf8-tgb2312 -otest2.xls test.xls


If mysql into outfile ERROR: Can't create/write to file ERROR occurs in linux, this problem is caused by permission issues.

Solution

The code is as follows:
Sudo vi/etc/apparmor. d/usr. sbin. mysqld

Add the/data writable directory in the directory as follows:

The code is as follows:
...
/Usr/sbin/mysqld {
...
/Var/log/mysql/r,
/Var/log/mysql/* rw,
/Var/run/mysqld. pid w,
/Var/run/mysqld. sock w,
/Data/* rw,
}

Then reload:

The code is as follows:
Sudo/etc/init. d/apparmor reload/restart

You also need to restart mysql. otherwise, some inexplicable errors may occur, such:
ERROR 1286 (42000) at line 1: Unknown table engine 'innodb'


Restart mysql:

The code is as follows:

Sudo service mysql stop
Sudo service mysql start

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.