How to export MySQL data directly into CSV

Last updated on 11/02/2015

If you want to export for example the node id(n.nid), title(n.title) and related gallery id(ga.field_gallery_adv_nid) from your MySQL Database directly into CSV file.

Before running the query, make sure the location you want to store your result file has write permissions by MySQL.

Here is the example code:

SELECT n.nid AS nid, n.title AS title, ga.field_gallery_adv_nid AS gallery_nid  INTO OUTFILE '/tmp/my_result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM  node n
INNER JOIN field_data_field_gallery_adv ga ON ga.field_gallery_adv_nid=n.nid
WHERE  (n.type = 'gallery_adv');