12 September 2006
Export a MySQL table to a CSV file
Posted by Mikhail Esteves under: LAMP; Tips .
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
4 Comments so far...
Cujo Says:
9 September 2007 at 6:31 pm.
Thanks
mortenb Says:
2 June 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;
Baz Says:
13 June 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).
valrossie Says:
4 October 2008 at 1:31 am.
First creat connection with:
- $host: this is the location for the Mysql server it can be a hostname or an ip adress. it is usualy localhost.
* $db: this is the Mysql user account used to access the database.
* $user: this is the password for the Mysql user account
* $pass: this is the name of the Mysql database used.
——————-
valrossie
messsage marketing