SQL Query that Create CSV file with Timestamp on it

一笑奈何 提交于 2020-04-18 06:14:09

问题


I created This shell script below, which fetches data from MySQL and create output as csv file. but I want to create this csv with a timestamp. so I just want filename to be like this result_ddmmyy.csv

Is there any way to do this from script or from SQL query.

#!/usr/bin/bash

#scirpt to connect with db

master_db_user='root'
master_db_passwd='123'
master_db_port='3306'
master_db_host='localhost'
master_db_name='sagar_tb'


#Preparing script 


#MySql Command to connect to a database 

mysql -u$master_db_user -p$master_db_passwd -D$master_db_name -e  'select * into outfile "/created_files/result931.csv" fields terminated by "," lines terminated by "\n" from sagar_tb.test_table;'

echo "End of the Script"

Thanks and Regards, Sagar Mandal


回答1:


Add below commands in your script and update here if it works

    now=$(date +"%y%m%d")

And later add this in your file creation

    "/created_files/result_${now}.csv"


来源:https://stackoverflow.com/questions/61039141/sql-query-that-create-csv-file-with-timestamp-on-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!