How to export a MySQL query as CSV

John John (304)
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.

Posted in these interests:
h/webdev60 guides
h/sysadmin12 guides
h/mysql9 guides

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

SELECT * FROM users

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'
SELECT *
FROM users
INTO OUTFILE '/tmp/user.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

If you found this guide useful, check out our guide on how to query by timestamp in MySQL.

A tabletop arcade cabinet full of Pi!
Ash Ash (362)
0

The Picade from Pimoroni brings new meaning to tabletop gaming. This tiny box is built for DIY gamers with a touch of class.