我想用命令直接从 mysql 导出 csv
SELECT ....
FROM ...
INTO OUTFILE '/tmp/export.csv'
FIELDS TERMINATED BY ','
ESCAPED BY '\\'
LINES TERMINATED BY '\n' ;
这工作完美,但编码不是utf8。如何使内容导出utf8编码?
如SELECT ... INTO
语法中所述:
SELECT ... INTO OUTFILE
是 的补码LOAD DATA INFILE
。列值写入转换为CHARACTER SET
子句中指定的字符集。如果不存在这样的子句,则使用binary
字符集转储值。实际上,没有字符集转换。如果结果集包含多个字符集中的列,则输出数据文件也会如此,您可能无法正确重新加载文件。
语法记录在SELECT
Syntax下:
[INTO OUTFILE 'file_name' [CHARACTER SET charset_name]
所以:
SELECT ....
FROM ...
INTO OUTFILE '/tmp/export.csv'
CHARACTER SET utf8
FIELDS TERMINATED BY ','
ESCAPED BY '\\'
LINES TERMINATED BY '\n' ;