前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql导出select结果到文件

mysql导出select结果到文件

作者头像
joshua317
发布2018-04-09 18:19:13
3.2K0
发布2018-04-09 18:19:13
举报
文章被收录于专栏:技术博文技术博文

mysql -hxx -uxx -pxx -e "query statement" db > file 

例如: 

mysql -h127.0.0.1 -uroot -p000000 -e"select * from a" test > 1.txt 

        host ip     user   password   query statement  database  filename 

这样会输出列名信息,如果不想输出列名信息: 

mysql -h127.0.0.1 -uroot -p000000 -N -e"select * from a" test > 1.txt 

        host ip     user   password   query statement  database  filename 

或 

mysql -hxxx -uxx -pxx 

select * from table into outfile 'xxx.txt'; 

例如: 

mysql -h127.0.0.1 -uroot -p000000 

select * from a into outfile '1.txt'; 

两种方法效果一样的 

第二种方式的mysql文档: 

SELECT [select options go here] INTO {OUTFILE | DUMPFILE} filename 

EXPORT_OPTIONS 

FROM table_references [additional select options go here] 

例如: 

mysql -h127.0.0.1 -uroot -p000000 

select * from a into outfile "1.txt" fields terminated by '\t' lines terminated by '\r\n' 

第一种方法和第二种方法的结合:使用 mysql -e执行导出到文件的sql语句 

mysql -hxx -uxx -pxx -e "query statement" db 

例如: 

mysql -h127.0.0.1 -uroot -p000000 -e"select * from a into outfile '1.txt' fields terminated by ',' lines terminated by '\r\n'" test 

如果不想输出列名信息: 

mysql -h127.0.0.1 -uroot -p000000 -N -e"select * from a into outfile '1.txt' fields terminated by ',' lines terminated by '\r\n'" test 

默认情况下, mysql -e导出的文件,列是用"\t"分隔,行是用"\r\n"分隔(dos),行是用"\n"分隔(unix 

追加一种方式: 

select col002,col005,col004,col008 into outfile 'e:/mysql/i0812.txt' fields terminated by '|' lines terminated by '\r\n' from a where col003 in (select col001 from qdbm) order by col005;

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-07-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档