How to save mysql query output into a file

Sometimes you may want to save output of some mysql query to a text file. Maybe even to Excel’s spreadsheet file so that you have more control with editing, sorting … MySQL offers many useful options there.

Below is an example on how to save some mysql query output to csv file. You can terminate fields with some key character which is super useful. This example has fields terminated with ; and lines with \n.

mysql> select firstname, lastname, email, phone from clients INTO OUTFILE '/tmp/outputfile.csv' FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n';

Just make sure that mysql has suitable permissions so that it will be able to write to a file – chmod 777.

© 2024 geegkytuts.net
Hosted by SIEL


About author