将当前时间添加到INTO OUTFILE'C:/Output/data_.csv'

时间:2018-05-10 13:16:19

标签: mysql windows timestamp

我正在寻找一种方法,可以在每次新导出后将当前时间添加到我的文件名中。

当前代码:

SELECT * INTO OUTFILE 'C:/Output/data_.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' FROM eloge_collector;

我想要实现的目标:

SELECT * INTO OUTFILE 'C:/Output/data_2018-05-10-15-14.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' FROM collector;

或类似的东西。

1 个答案:

答案 0 :(得分:1)

您可以使用预准备语句创建查询字符串然后执行它 https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html

这是我的解决方案

首先创建create变量来保存查询字符串。

使用concat函数将当前日期时间注入查询字符串。

使用DATE_FORMAT,现在用于获取当前日期时间

set @sql = concat("SELECT * INTO OUTFILE 'C:/filePrefix_",DATE_FORMAT(NOW(), '%Y%m%d%H%i%s'),".fileExtension' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' FROM `table`");
prepare s1 from @sql; --create statment from variable 
execute s1; -- execute prepared statements