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 |