How To Output MySQL Query Results In CSV Format


Short Answer

1
2
3
4
5
6
SELECT *
INTO OUTFILE '/tmp/results.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM TABLE

This query writes results into a CSV file on the server that is running MySQL. Column names will not be exported.

Each field will be encouled in double quotes, the fields will be separated by commas, and each row will be output on a new line separated by \n. Sample output of this query would look like:

1
2
3
"1","Clark","Sales"
"2","Dave","Accounting"
"3","Ava","Sales"




Related Posts

How To Specify Unique Constraint For Multiple Columns In MySQL

How to make multiple columns unique (together) in an existing...

How To Select One Row Per ID With Max Value On A Column

Suppose we have the following REIMBURSEMENT table, find the max...

How To Get The Size Of A Table In MySQL

How to get the size of a table in MySQL...

How To Get The Size Of A Database In MySQL

How to get the size of a database in MySQL...

How To Concatenate Multiple Rows Into One Field In MySQL

How to concatenate multiple rows into one field in MySQL...

How To Change The Data Type Of A Column In MySQL

Given a table EMPLOYEE, change the data type of the...