全国统一服务热线:400-633-9193

mysql实现查询结果导出csv文件及导入csv文件到数据库操作

    网络     2018-07-16    1261

本文实例讲述了mysql实现查询结果导出csv文件及导入csv文件到数据库操作。分享给大家供大家参考,具体如下:

mysql 查询结果导出csv文件:

1
2
3
4
5
6
7
select logtime, operatingsystem, imei
from GameCenterLogs
where
  operatingsystem >= 1 and operatingsystem <=3
group by operatingsystem,imei
into outfile '/tmp_logs/tmp.csv'
fields TERMINATED BY ',' OPTIONALLY ENCLOSED BY '#' LINES TERMINATED BY '\r\n'

fields TERMINATED BY ',' 设置字段的分割符

OPTIONALLY ENCLOSED BY '#' 设置字段内容若为字符串,则使用'#'包含

LINES TERMINATED BY '\r\n' 数据行分割符

导出文件内容:

1453513680,3,#hello word#\r\n
1453515470,2,#title content#\r\n

mysql命令行导入csv文件到数据库:

1
2
load data infile '/tmp_logs/tmp.csv'
into table GameCenterDAULogs fields terminated by ',' OPTIONALLY ENCLOSED BY '#' lines terminated by '\r\n'

有可能操作系统不同,导出的csv文件的数据行分隔符不一定为\r\n,可以使用cat -A /tmp_logs/tmp.csv 查看结尾符


  分享到:  
0.2484s