8 May 2018

Example query for direct csv saving from mysql. Using query to import or export data is the fastest method. You can export your query results as csv with custom syntax. You have to use union to join headers if you need headers. Also you need write permissions for mysql to write the csv into the directory. Usually web directory has access for mysql out of the box.

Source code viewer
  1. /* Add column headers. */
  2. SELECT 'First column header', 'Second column header'
  3. /* Union for adding column headers with data. */
  4. /* Your query for the results. */
  5. SELECT * FROM my_table where my_column_name LIKE 'my_condition'
  6. /* Data for the exported csv file, use directory that has write permissions for mysqlserver. */
  7. INTO OUTFILE '/var/www/my_file.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Programming Language: MySQL