MySQL – Adding Date/Time Stamp to Automated File Naming

MySQLmysql-workbench

   SELECT 
'"Part #"',
'"Manu"',
'"Qty"',
'"partdesc"',
'"Rohs status"'
UNION all 
(
SELECT partno, manuf, qtyleft, partdesc, stfifo.ROHScompliance
    FROM stmaster, stfifo
    WHERE stmaster.internal = stfifo.internal AND stfifo.warehouse = 'MAIN'

AND manuf != 'aaaa' 
AND manuf <>'bbbb' 
AND manuf <>'cccc' 

AND stmaster.partno <>'yyyyyyyyy' 
AND stmaster.partno <>'xxxxxxxx' 
AND QTY >0
GROUP BY partno, manuf
ORDER BY partno ASC
INTO OUTFILE
    'D:/Reporting/abc - 19.03.15.xls' 
    FIELDS TERMINATED BY '  ' ENCLOSED BY '' LINES TERMINATED BY ''

How do I make it so that the resulting file includes today's date as determined by a NOW() function? CONCAT() functions seem useless as the file is within a UNION all() function. At the moment I cannot run this query more than once without deleting the .xls file first. Any other workarounds are greatly welcomed. Thanks in advance

Best Answer

You may need to use shell script for this.

First write a script to do so.. a sample is as below

!/bin/bash

dto=date +%d'_'%b'_'%Y'_'%H_%M_%S
mysql -uroot -p dbname -e " select * from table_name into outfile 'file1_$dto' "

This will always give you unique names against your files.

Hope it helps.