Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

How to Save results from MySQL into an external file

1. From the MySQL command line

SELECT columns FROM table_name WHERE whatever='something' INTO OUTFILE "/tmp/outfile.txt";
 

2. From the command line

2.A. Piping the SQL into the database

echo "SELECT columns FROM table_name WHERE whatever='something'" | /path/to/mysql -uUSERNAME -pPASSWORD DATABASENAME > /tmp/outfile.txt;
 

2.B. Using an external SQL file.

/path/to/mysql -uUSERNAME -pPASSWORD DATABASENAME < /tmp/sql.sql > /tmp/outfile.txt;

Share this!