Using Into Outfile in MySQL to Dump Query Results to Text

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:

mysql> SELECT *
FROM table_name
WHERE table_id = 1
INTO OUTFILE "/tmp/sample_outfile.txt"
FIELDS TERMINATED BY "|"
LINES TERMINATED BY "\n\n=============\n\n";

| Tags:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options