Export to CSV with MySQL

On Ubuntu, this will create a file in /var/lib/mysql/[DB_NAME]/output.csv

I had to run the query as MySQL root. Didn’t try to find out what permission actually permitted the user to write to the file. It’s probably create table though.


SELECT
v.id, v.title, vc.name, v.slug, v.description
FROM
video v
INNER JOIN
video_category vc ON v.video_category_id = vc.id
INTO
OUTFILE 'output.csv'
FIELDS ESCAPED BY '"'
TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';

Leave a Comment