Data in MySQL exported as Excel file statement

Source: Internet
Author: User
The code is as follows Copy Code

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

Export to TXT file:

The code is as follows Copy Code

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


By default, the tab is used to divide fields, and records use N segmentation. You can customize three parameters.

The code is as follows Copy Code

SELECT * Fromhx_9enjoy into outfile '/tmp/9enjoy_hx.txt ' FIELDS terminated by ', ' optionallyenclosed by ' "' LINES Terminat ED by ' n ';

Use, split fields, double quotes to qualify field contents, N Split records.

You can also specify record output for certain conditions:

The code is as follows Copy Code

SELECT * Fromhx_9enjoy WHERE ID < into outfile '/tmp/9enjoy_100.txt '

If there are garbled characters in Excel when you open it we can export the set encoding for gb2312 to use Iconv for encoding conversion on the server side:

The code is as follows Copy Code

Iconv-futf8-tgb2312-otest2.xls Test.xls


if Linux may encounter MySQL into outfile error:can ' t create/write to file error, this problem is the issue of permissions

Solutions

The code is as follows Copy Code
sudo vi/etc/apparmor.d/usr.sbin.mysqld

Add the/data writable directory inside, as follows:

The code is as follows Copy Code
...
/usr/sbin/mysqld {
...
/VAR/LOG/MYSQL/R,
/var/log/mysql/* RW,
/var/run/mysqld/mysqld.pid W,
/var/run/mysqld/mysqld.sock W,
/data/* RW,
}

Then reload:

The code is as follows Copy Code
Sudo/etc/init.d/apparmor Reload/restart

You also need to restart MySQL, or there will be some inexplicable errors, such as:
ERROR 1286 (42000) at line 1:unknown table engine ' InnoDB '


Restart MySQL:

  code is as follows copy code

 sudo Service MySQL stop
 sudo service MySQL start

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.