Export a MySQL table to a CSV file

To export a MySQL table as a CSV file, you can use the following SQL statement:

select uniqName, uniqCity, uniqComments
into outfile 'uniq.csv'
fields terminated by ',' optionally enclosed by '"'
lines terminated by '\n'
from tblUniq

And you’ll find your table data sitting in uniq.csv.

To learn how to import a CSV file into a MySQL table, check this post

  • Share/Bookmark

3 Comments

CujoSeptember 9th, 2007 at 6:31 pm

Thanks

mortenbJune 2nd, 2008 at 2:18 pm

Do not forget to grant FILE to the user.
as mysql root user:
mysql>grant file to ‘’ @’‘ identified by ‘’;
mysql>flush privileges;
mysql>\q;

BazJune 13th, 2008 at 2:20 pm

Thanks for this – has helped me out greatly.

However, it’s also worth noting that (on my OSX box at least) the output file is placed in /usr/local/mysql/data/[DATABASE]/ – and normal users don’t have permission to access the data folder (so it also didn’t show up when I was doing a search).

Leave a comment

Your comment