Alex Rivera | Logout

MySQL Results to a File

Asked 2008-08-27T18:48:04.970
12

How do I write the results from a mysql query to file? I just need something quick. Output can be CSV, XML, HTML, etc.

Edit
Report

1 Answer

20
SELECT a,b,a+b 
  FROM test_table
  INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'

(the docs show INTO OUTFILE up in the SELECT .. portion which may work as well, but I've never tried it that way) http://dev.mysql.com/doc/refman/5.0/en/select.html

INTO OUTFILE creates a file on the server; if you are on a client and want it there, do:

mysql -u you -p -e "SELECT ..." >  file_name 
answered 2008-08-27T18:50:42.373

Your Answer