MySQL支持将查询结果直接导出为文本格式,格式如下:
into outfile ‘导出的目录和文件名’ 指定导出的目录和文件名
fields terminated by ‘字段间分隔符’ 定义字段间的分隔符
optionally enclosed by ‘字段包围符’ 定义包围字段的字符(数值型字段无效)
lines terminated by ‘行间分隔符’ 定义每行的分隔符
举个栗子:
select * from [表名] where [字段名] ='条件'
into outfile 'c:/test.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';
执行后,会把指定表中记录数据导出到c:/test.csv文件中。每个字段以,(逗号)分隔,字段内容是字符串的以”(双引号)包围,每条记录使用\r\n换行。如图所示
如果导出的过程中出现:
the MySQL server is running with the --secure-file-priv option so it cannot execute this statement
首先了解一下secure-file-priv特性