MySQL export to CSV

MySQL allows you to export a query directly in to a CSV file that can be opened by Microsoft Excel.

This is the basic syntax:

1
SELECT FIELDS FROM TABLE INTO OUTFILE '/path/to/file_name.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';

The export file that is created “file_name.csv” must not already exist, the MySQL user must have write permissions to the directory the file will be created in, and the FILE permission within MySQL.

See the MySQL manual: SELECT Syntax

Leave a Reply