Save MySQL query results into a text or CSV file

Contributor Icon Contributed by qmchenry Date Icon June 28, 2006  
Tag Icon Tagged: MySQL

MySQL provides an easy mechanism for writing the results of a select statement into a text file on the server. Using extended options of the INTO OUTFILE nomenclature, it is possible to create a comma separated value (CSV) which can be imported into a spreadsheet application such as OpenOffice or Excel or any other applciation which accepts data in CSV format.


Given a query such as

SELECT order_id,product_name,qty FROM orders

which returns three columns of data, the results can be placed into the file /tmo/orders.txt using the query:

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.txt'

This will create a tab-separated file, each row on its own line. To alter this behavior, it is possible to add modifiers to the query:

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

In this example, each field will be enclosed in “double quotes,” the fields will be separated by commas, and each row will be output on a new line separated by a newline (\n). Sample output of this command would look like:

"1","Tech-Recipes sock puppet","14.95"
"2","Tech-Recipes chef's hat","18.95"
...

Keep in mind that the output file must not already exist and that the user MySQL is running as has write permissions to the directory MySQL is attempting to write the file to.

Previous recipe | Next recipe |
 
  • Thanks for this, a MySQL noob like myself always forgets the exact syntax!

    Bookmarked.
  • Thanks for the helpful post but my problem is that when i execute the query and go to the location where i saved the file, i can't find any file there. However if i again try the same command it says "File already exists" but actually there is not. Wha'ts wrong?
  • Amit
    I am facing the same problem. Did you find the answer?
  • Heini
    Just like this:

    SELECT order_id,product_name,qty
    FROM orders
    INTO OUTFILE 'c:/tmp/orders.csv'

    Just type in the drive you want.
  • Udit Chaudhary
    This happens only if you have executed the query twice and redirecting it to same location with same name otherwise rest is fine.
  • Sven
    I got the same problem when I connect to a remote database server. The file is created on the remote server.

    Anyone know how to get this file created on the local host?
  • Sumesh
    Is there a similar thing available in oracle?
  • How can I used a a date value in the output file?
  • jai
    I dont know y the new line operator is not at all working in my MySql...
    am using version 5.0...


    could some one plz help me out................
  • shiv`
    can u tell me how can i get the column names along wtiht the result set
  • danishm
    For oracle ,you can use toad
  • palcb
    "Thanks for this, a MySQL noob like myself always forgets the exact syntax!

    Bookmarked."

    it was those people who memorized syntax or function than understanding what a language can do or cant do was noob.
  • augusto
    how a very few lines may save lots of time.....
    thanks a lot!
  • regbailey
    Thank you very much indeed. Your article has just saved me a tremdous amount of time, trouble & effort. An excellent example of the web being used as it should be.

    Many thanks again.

    Reg Bailey.
  • regbailey
    Hi,

    Also discovered that including the 'optionally' keyword, eg:

    ... optionally enclosed by '"' ...

    that MySQL will decide which fields (eg. text, char etc) need enclosing in double quotes and which fields (eg. int, float etc) do not in order to comply with strict CSV format. Depending on your table make-up & size, this too is a great time saver. Mant thanks again for pointing me (and many others) in the right direction.

    Reg.
  • aaruthra
    it was very useful buddy..but i need how to save the commands given by me... :)
  • velmuruganm
    Incorrect syntax near the keyword 'into'.
  • velmuruganm
    thanks.....
    this is more useful to my study..........
    once again my hearty thanks.........
  • SELECT INTO OUTFILE Save the result on the server. If you are on a remote MySQL Console, you can use the following to save the result locally

    \T logfile
    to log it to logfile. then use
    \t
    to turn off logging

    http://dev.mysql.com/doc/refman/5.0/en/mysql-co...
blog comments powered by Disqus