Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Save MySQL query results into a text or CSV file

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 439
    Comment on it

    Hello Readers!

    MySQL provides an easy mechanism for writing the results of a select statement into a text file on the server.

    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 /folder_name/orders.txt using the query:

    SELECT order_id,product_name,qty FROM orders
    INTO OUTFILE '/folder_name/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 '/folder_name/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"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: