Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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 5

    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.