Use the following command to export the query rows to a file on the server in the CSV format.
SELECT * FROM `table` INTO OUTFILE '/var/www/file.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Source:
http://stackoverflow.com/questions/356578/how-to-output-mysql-query-results-in-csv-format