How to export a MySQL query as CSV

  • by Tyler (252)
  • Time to complete: 1 minute

This guide will show you how to export your MySQL query as a CSV file, a common need when analyzing records or data.

1

For instance, if you're trying to get all of the users from the 'users' table:

SELECT * FROM users
2

Append this onto the end of the query to output the results to a file /tmp/users.csv

INTO OUTFILE '/tmp/users.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
3

SELECT *
FROM users
INTO OUTFILE '/tmp/user.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'