MySQL select into outfile用法

在使用mysql的into outfile命令将数据库数据导出为csv、excel时,发现数据可以导出,但是列名却没有,但是却可以通过修改sql语句的方法加上表头,具体实现方法是在查找数据的时候并上列名: 
不带表头

select uid,nickname,realname,mobile,idcard from  m order by m.uid desc into outfile '/var/www/api/public/a/download/".$file_name."' fields terminated by ',' optionally enclosed by '\"' escaped by '\"' lines terminated by '\r\n' 
  •  

带上表头

select * into outfile 'e:/555.xlsx'  fields terminated by '\\t' OPTIONALLY ENCLOSED BY '"' lines terminated by '\\n' from (select '姓名','性别','年龄' union select username,sex,age from table) b;

 使用mysql导入

 

LOAD DATA INFILE '/tmp/fi.txt' INTO TABLE test.fii FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

而为了满足某种特别的需求,会采用不同的分割方式。支持,在导出数据时,设置记录,与字段的分隔符。通过如下的选项:

fields:设置字段选项    Lines:设置行选项(记录选项)

字段:field terminated by '\t'

记录:lines terminated by '\n' starting by ''(每一行开始是空字符串);

FIELDS TERMINATED BY ',' 字段间分割符
OPTIONALLY ENCLOSED BY '"' 将字段包围 对数值型无效
LINES TERMINATED BY '\n' 换行符 

注:常规的,所有的记录,应该通过行来显示,例外是保存二进制数据:

Blob binary

使用into dumpfile

 select * into dumpfile 'f:/mysql/test/six' from teacher_class limit 1;

 

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值