Everytime I need to do this I go into at least five minutes of Googling for a decent solution. The problem is that a lot of people don’t have access to the commandline, so the solutions all reference PHPMyAdmin which is, you know, OK, but I don’t use it or want to install it just to export some quick reports. Other solutions are similarly indirect.
Anyway, for my own benefit, here’s the solution I found yesterday:
SELECT
some, columns, from, my, table
FROM
a_table
WHERE
whatever = 'something'
INTO OUTFILE '/tmp/outfile.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"';
This seemed to do the job, although I did have a mild concern that it might not be escaping “stuff” properly. We shall see.
Leave a Reply