Saturday, August 20, 2016

MySQL query results to a CSV file

You can use this syntax to output your MySQL query results to a CSV file, for example:

SELECT blog_id, path, registered, last_updated 
    INTO OUTFILE '/tmp/test.csv' 
    FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY '"' 
    ESCAPED BY '\\' 
    LINES TERMINATED BY '\n' 
FROM wp_blogs 
WHERE path IN ('/105102/','/105095/','/103206/');


Note: It's better to output the file to /tmp directory because mysql user has permission to write there.