mysql导出为CSV的同时加上表头

今天在使用mysql的into outfile命令将数据库数据导出为csv时,发现数据可以导出,但是列名却没有,但是却可以通过修改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 uid,nickname,realname,mobile,idcard from( select uid,nickname,realname,mobile,idcard from ims_mc_members union select 'uid','昵称','真实姓名','手机号', '身份证号') 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'

文章来源:http://www.turtletl.com

你可能感兴趣的:(IT)