Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting results of a Mysql query to excel?

My requirement is to store the entire results of the query

SELECT * FROM document  WHERE documentid IN (SELECT * FROM TaskResult WHERE taskResult = 2429) 

to an Excel file.

like image 338
Priya Avatar asked Apr 24 '12 09:04

Priya


People also ask

Can we export SQL query results to Excel?

Method Number 1 – Copy Grid results and paste into Excel Right-click on the database you want to export from. Then Select tasks and “Export Data”. The SQL Server Wizard will startup. Click Next through the prompts.

How do I export SQL query results to Excel command line?

To start to use this feature, go to Object Explorer, right click on any database (e.g. AdventureworksDW2016CTP3), under the Tasks, choose Export Data command: This will open the SQL Server Import and Export Wizard window: To proceed with exporting SQL Server data to an Excel file, click the Next button.

How do you output MySQL query results to a file?

Save MySQL Results to a File There's a built-in MySQL output to file feature as part of the SELECT statement. We simply add the words INTO OUTFILE, followed by a filename, to the end of the SELECT statement. For example: SELECT id, first_name, last_name FROM customer INTO OUTFILE '/temp/myoutput.


2 Answers

The typical way to achieve this is to export to CSV and then load the CSV into Excel.
You can using any MySQL command line tool to do this by including the INTO OUTFILE clause on your SELECT statement:

SELECT ... FROM ... WHERE ...  INTO OUTFILE 'file.csv' FIELDS TERMINATED BY ',' 

See this link for detailed options.

Alternatively, you can use mysqldump to store dump into a separated value format using the --tab option, see this link.

mysqldump -u<user> -p<password> -h<host> --where=jtaskResult=2429 --tab=<file.csv> <database> TaskResult 

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';.

like image 187
Roland Bouman Avatar answered Sep 30 '22 09:09

Roland Bouman


Good Example can be when incase of writing it after the end of your query if you have joins or where close :

 select 'idPago','fecha','lead','idAlumno','idTipoPago','idGpo'  union all (select id_control_pagos, fecha, lead, id_alumno, id_concepto_pago, id_Gpo,id_Taller, id_docente, Pagoimporte, NoFactura, FacturaImporte, Mensualidad_No, FormaPago, Observaciones from control_pagos into outfile 'c:\\data.csv'  FIELDS TERMINATED BY ','  OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'); 
like image 34
Daniel Adenew Avatar answered Sep 30 '22 10:09

Daniel Adenew