Sending Query Results to a File

# Dump a query as comma-separated values SELECT * FROM author   INTO OUTFILE '/tmp/author'     FIELDS TERMINATED BY ','     ENCLOSED BY '"'     LINES TERMINATED BY '\n'; 

If desired, you can quickly write a table out to a file on disk using the INTO OUTFILE clause of the SELECT statement. The default behavior of the clause, when used in its simplest form of SELECT ... INTO OUTFILE '/some/file/name', is to write out the file in tab-separated value format, with each field separated by a tab and each row separated by a newline.

INTO OUTFILE can accept additional options to control the format of the file that is written. The example query in the phrasebox outputs one style of commaseparated value (CSV) file.

The options are fairly self-explanatory for experienced programmers but might be a little baffling for novices. The important thing to remember is that character sequences such as '\n' and '\t' have a special meaninginstead of representing a literal backslash followed by a lower case n or t, they are interpreted by MySQL to mean newline or tab.

For more detailed information on how to control the format of the file generated, see the SELECT page of the MySQL online manual (

MySQL Phrasebook. Essential Code and Commands
MySQL Phrasebook
ISBN: 0672328399
EAN: 2147483647
Year: 2003
Pages: 130

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: