Just a post for future reference:

Writing Query Results to a File in MySQL

Blogging this mostly for my own reference, but I had to write query results to a file in MySQL today and hadn’t ever had to do that before. Luckily it’s darn simple. The following will write out a CSV with field data enclosed with quotation marks (which is important if the data itself may contain a comma).

SELECT whatever FROM whereever
INTO OUTFILE ‘/path/to/file.csv’
FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\n’

You can then open that up in your favorite spreadsheet program quite easily