mysql导出csv/excel文件的几种方法,mysql的load导入csv数据

时间:2023-03-09 16:47:55
mysql导出csv/excel文件的几种方法,mysql的load导入csv数据

方法一

php教程用mysql的命令和shell

select * into outfile './bestlovesky.xls' from bestlovesky where 1 order by id desc  limit 0, 50;

方法二 把bestlovesky.xls以文本方式打开,然后另存为,在编码选择ansi编码,保存

echo "select id,name from bestlovesky where 1 order by id desc limit 0, 50;"| /usr/local/mysql/bin/mysql -h127.0.0.1-uroot -p123456 > /data/bestlovesky.xls

方法三

在本地直接导出服务器数据:

mysql DB_name  -h10.36.10.11 -utest -p -e "select * from TABLE_name where acti_type <> '无效';  order by acti_date desc" > ./test.txt

-N 即可 -N, --skip-column-names.Don't write column names in results.

默认带有字段名/列名,不输出列名使用-N参数。

因为office默认的是gb2312编码,服务器端生成的很有可能是utf-8编码,这个时候你有两种选择,1.在服务器端使用iconv来进行编码转换

iconv -futf8 -tgb2312 -otest2.xls test.xls
如果转换顺利,那么从server上下载下来就可以使用了。
转换如果不顺利,则会提示:iconv: illegal input sequence at position 1841 类似于这样的错误,
先把test.xls下载下来,这个时候文件是utf-8编码的,用excel打开,乱码。
把test.xls以文本方式打开,然后另存为,在编码选择ANSI编码,保存。
mysql的load导入csv数据:

load data infile '/home/mysql/all_0914.csv'
into table `DB_name`
character set utf8
fields terminated by '^'
lines terminated by '\n';