Link to home
Start Free TrialLog in
Avatar of OmegaBiz
OmegaBiz

asked on

How-To: MySQL Query Output to CSV file with date format and send to SFTP server in Linux Environment?

Greetings,

I need a solution to send a MySQL query to csv file with date format then it needs to be uploaded to a SFTP server for users to retrieve. Currently I have the following:

mysql -u user1 -pP@ssword -hnodes1.aws.com <script1.sql> > test1.csv

So I need the test1.csv compressed with a name and data format or I need the test1.csv produced with as a file name with data format. Please advise.
Avatar of JesterToo
JesterToo
Flag of United States of America image

This is potentially a very complex task depending on the data being produced by your query (commas or quotes or escape characters or blanks in some data fields).

Take a look at a very in-depth discussion and several potential solutions at this page:
     
   http://stackoverflow.com/questions/356578/how-to-output-mysql-query-results-in-csv-format
Avatar of skullnobrains
skullnobrains

mysql can export directly to a csv file on the server using select into outfile.

if you want to grab a csv from a remote location, you need to run your select query in a language such as php, perl, ruby, python... and perform the export manually.

if the data consists of simple words with no stuff such as tabs or line breaks, you can also use a simple shell sed / awk script. you'd basically need to transform tabs into commas, and possibly add quotes as well if you want the output csv to be quoted.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.