Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Is it possible to output MySQL query results in csv format?

Damian99Damian99 USAPosts: 116Member

In my project it is required to output MySQL query results in csv format for reporting purposes. I couldn't find a proper way to do this. Is it possible?

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member
    edited February 2014

    From: http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/

    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'
    
Sign In or Register to comment.