To save query results to a textfile, use the INTO OUTFILE command at the end of the query. You can also use FIELDS TERMINATED BY "" and LINES TERMINATED BY "" to change the output of the file. You must place the outfile in a directory that is writeable by the MySQL user, so make sure to point the file location to /tmp/. Here's a sample query:
