Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Results to a File

Tags:

database

mysql

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

like image 671
tghw Avatar asked Aug 27 '08 18:08

tghw


People also ask

How do I save SQL output to a file?

Click Query, and then click Results to File. Enter and then execute the SQL statement. In the Save Results dialog box, specify the following settings: Save In: Select a directory in which to save the file.

How do I store SQL query results in a text file?

However, if you prefer to export SQL query results to a text file via a Wizard, we have your back. To begin with, right-click the database in SQL Server Management Studio or SSMS. Then, select the Import or Export data option and head to Export Data under Tasks. Next, open the SQL Server Import and Export wizard.


2 Answers

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 
like image 157
Matt Rogish Avatar answered Sep 30 '22 20:09

Matt Rogish


if you have phpMyAdmin installed, it is a nobrainer: Run the query (haven't got a copy loaded, so I can't tell you the details, but it really is easy) and check neer bottom for export options. CSV will be listed, but I think you can also have SQL if you like :)

phpMyAdmin will give CSV in Excels dialect, which is probably what you want...

like image 37
Daren Thomas Avatar answered Sep 30 '22 18:09

Daren Thomas