Export data to CSV from MySQL


Many times I get request from client for 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 write permissions to the location where you want to store the results file.

You can either download the csv file from server's '/tmp' directory, or you can move this to http root directory and send link to client to download.
cd /tmp/
mv data_dump_member.csv /var/www/html/
gzip -9 data_dump_member.csv

Comments

Popular posts from this blog

Detecting user's screen size and resolution

autoload class

MySql Slow Queries