How to save MySQL query output to excel or .txt file?

MysqlExcel

Mysql Problem Overview


How do you save output of a MySQL query to a MS Excel sheet?

Even if it's only possible to store the data in a .txt file, it will be okay.

Mysql Solutions


Solution 1 - Mysql

From 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 application 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 /tmp/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.

Syntax

   SELECT Your_Column_Name
    FROM Your_Table_Name
    INTO OUTFILE 'Filename.csv'
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'

Or you could try to grab the output via the client:

> You could try executing the query from the your local client and > redirect the output to a local file destination:

mysql -user -pass -e "select cols from table where cols not null" > /tmp/output

Hint: If you don't specify an absoulte path but use something like INTO OUTFILE 'output.csv' or INTO OUTFILE './output.csv', it will store the output file to the directory specified by show variables like 'datadir';.

Solution 2 - Mysql

You can write following codes to achieve this task:

SELECT ... FROM ... WHERE ... 
INTO OUTFILE 'textfile.csv'
FIELDS TERMINATED BY '|'

It export the result to CSV and then export it to excel sheet.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrɑːdʒɑView Question on Stackoverflow
Solution 1 - MysqlAmarnath BalasubramanianView Answer on Stackoverflow
Solution 2 - MysqlCharles StevensView Answer on Stackoverflow