从mysql中导出csv格式的数据
===================从数据库中取数据导出到execl文档中=====================
连接数据库:mysql -h x.x.x.x -u ceshi -plianxi
show databases;
use 你的库名
show tables;
select 指标1,指标2,指标3,timestamp,from_unixtime(timestamp) from 表名 where 表中的某个指标 in ("指标中的某个具体的值") and timestamp>=unix_timestamp("1970-05-01 00:00:00") and timestamp<unix_timestamp("1970-06-01 00:00:00") into outfile "/tmp/temp.csv";
例:我从liuliang表中导出指定机器x.x.x.x的上下行流量数据到文件/tmp/temp-liuliang.csv中
select hostname,inbps,outbps,timestamp,from_unixtime(timestamp) from liuliang where hostname in ("x.x.x.x") and timestamp>=unix_timestamp("2020-02-18 00:00:00") and timestamp<unix_timestamp("2020-02-19 00:00:00") into outfile "/tmp/temp-liuliang.csv";
导出数据到文件后,需要将文件中的空格间隔替换为逗号:"," (要确保文件中的空格全部被替换了,否则一直执行如下的命令)
vim命令打开文件后直接输入内容--->: :%s/\s\+/,/
: :%s+;+,+ 此格式为将分号";"替换为逗号","