Skip to content

OUTFILE

SELECT column1, column2
FROM table_name
INTO OUTFILE '/tmp/outfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
/path/to/yourfile.csv

The code excerpt above shows how to output the result of a SQL query to a CSV file using MySQL’s OUTFILE function. The INTO OUTFILE clause specifies the destination of the exported CSV file, while FIELDS TERMINATED BY,ENCLOSED BY, and LINES TERMINATED BY define the CSV’s formatting. This snippet exports column1 and column2 from table_name into a CSV file named outfile.csv, located in the /tmp directory.