Wednesday, 4 November 2020

How to output MySQL query results in CSV format?

 How to output MySQL query results in CSV format?

 

From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/


SELECT order_id,product_name,qty
FROM orders
WHERE foo = 'bar'
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Using this command columns names will not be exported.

Also note that /var/lib/mysql-files/orders.csv will be on the server that is running MySQL. The user that the MySQL process is running under must have permissions to write to the directory chosen, or the command will fail.

If you want to write output to your local machine from a remote server (especially a hosted or virtualize machine such as Heroku or Amazon RDS), this solution is not suitable.

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive