Export MySQL SELECT Query into CSV File

Leave a Comment
Sometimes we need to export MySQL SELECT query results into CSV format for testing or some other purpose. Here we are going to see how to output MySQL query results in CSV format.

We are going to use SELECT ... INTO OUTFILE MySQL query to store MySQL SELECT query results into CSV format.

 This SELECT ... INTO form of SELECT enables a query result to be stored in variables or written to a file: We can use SELECT ... INTO in following three way.
 SELECT ... INTO var_list selects column values and stores them into variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and line terminators can be specified to produce a specific output format.
 SELECT ... INTO DUMPFILE writes a single row to a file without any formatting. 

Here is the SELECT ... INTO OUTFILE MySQL query outputs the results into CSV file in the specified location.

SELECT id, country, capital FROM `countries`
INTO OUTFILE 'C:/Users/muni/Desktop/countries.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

 

0 comments:

Post a Comment