Outputting MySQL results to a CSV file without OUTFILE access

The MySQL client’s -e (or –execute=) flag allows you to specify a query to be run at the command line, which can then be output back to a local file:

$ mysql -h server.com -u username -p -D database -e "SELECT user_id FROM users ORDER BY user_id" > output.tsv

Note that by default this will output a tab-separated value file (TSV), not a comma-separated value (CSV) file.

Leave a Reply

Your email address will not be published. Required fields are marked *