SELECT...INTO OUTFILE导出数据到文件mysql
使用SELECT...INTO OUTFILE能够导出数据到文件,同时指定每一个字段的分隔符和每行的换行符,以下,sql
mysql> select id, name, price from book into outfile '/tmp/book_data.txt' fields terminated by',' lines terminated by '\r\n'; Query OK, 4 rows affected (0.00 sec) mysql>
以下是文本中数据的格式,数据库
1,Hello World,1.2 2,hello world book,1.2 3,hello world book,1.2 4,hello world book,1.2
能够看到使用逗号来分隔每一个字段,换行符是\r\n。函数
但有时候登陆不到数据库的主机上,能够使用mysql -e命令导出咱们想要的数据,以下,code
➜ ~ mysql -h localhost -u root -p034039 -e 'select id, name, price from account.book' > book_data.txt mysql: [Warning] Using a password on the command line interface can be insecure.
其实这使用的Linux的IO重定向。打开文件,能够看到以下数据,登录
id name price 1 Hello World 1.2 2 hello world book 1.2 3 hello world book 1.2 4 hello world book 1.2
其实就是sql->select id, name, price from account.book的查询结果,而后输出到文件中的。 可是字段的分隔还不是很清晰明了,那怎么办? 能够经过concat 函数链接多个字段为一行,而后中间加上分隔符,这样就清晰了,这样写sql,以下,file
➜ ~ mysql -h localhost -u root -p034039 -e 'select concat(id,"|",name,"|",price) from account.book' > book_data.txt mysql: [Warning] Using a password on the command line interface can be insecure.
输出的文件格式为:select
concat(id,"|",name,"|",price) 1|Hello World|1.2 2|hello world book|1.2 3|hello world book|1.2 4|hello world book|1.2
========END========command