Database

QUERY to Export MySQL Database to an Excel File

If you already have a table and want to export it to an Excel file, then you can simply use below script and apply it as query :

SELECT * FROM tablename
INTO OUTFILE ‘C:/myfile.csv’ FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’

Above query will produce an excel file named myfile.csv in C:\ Drive. Simply replace the first row with your desired sql query.

*) Notes: if you want to use dynamic file name instead of myfile.csv, then you should read this article.

Anyway, did you notice inside your .csv file there is no column header?,  to make your exported csv file is human readable it is better to put column header inside, read the tutorial here.

Leave a Reply

2 Comments

[…] previous article i have introduced you how to Export MySQL Database to an Excel File using a single simple […]

[…] you know from previous article, standard query used to export mysql database to an excel csv file has no column header […]