Export data to CSV from MySQL

Export data to CSV from MySQL,MySQL
Share it:

Export data to CSV from MySQL


Many times I get requests from the client for the data dump. I use MySQL database, and in MySQL, it is quite
easy to create CSV files directly from MySQL with just one query!

Let's say you want to export the email and name fields from your member table to a CSV file. Here is your code:

SELECT email, name INTO OUTFILE '/tmp/data_dump_member.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM member

Note: Make sure your MySQL server has to write permissions to the location where you want to store the results file.

You can either download the CSV file from the server's '/tmp' directory, or you can move this to the HTTP root directory and send a link to the client to download.
cd /tmp/

mv data_dump_member.csv /var/www/html/
gzip -9 data_dump_member.csv


Share it:

MySQL

Post A Comment:

0 comments: