Exporting results of a Mysql query to excel?

后端 未结 5 1532
有刺的猬
有刺的猬 2020-11-29 00:36

My requirement is to store the entire results of the query

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


        
相关标签:
5条回答
  • 2020-11-29 01:02

    This is an old question, but it's still one of the first results on Google. The fastest way to do this is to link MySQL directly to Excel using ODBC queries or MySQL For Excel. The latter was mentioned in a comment to the OP, but I felt it really deserved its own answer because exporting to CSV is not the most efficient way to achieve this.

    ODBC Queries - This is a little bit more complicated to setup, but it's a lot more flexible. For example, the MySQL For Excel add-in doesn't allow you to use WHERE clauses in the query expressions. The flexibility of this method also allows you to use the data in more complex ways.

    MySQL For Excel - Use this add-in if you don't need to do anything complex with the query or if you need to get something accomplished quickly and easily. You can make views in your database to workaround some of the query limitations.

    0 讨论(0)
  • 2020-11-29 01:06

    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
    
    0 讨论(0)
  • 2020-11-29 01:07

    In my case, I need to dump the sql result into a file on the client side. This is the most typical use case to off load data from the database. In many situations, you don't have access to the server or don't want to write your result to the server.

    mysql -h hostname -u username -ppwd -e "mysql simple sql statement that last for less than a line" DATABASE_NAME > outputfile_on_the.client
    

    The problem comes when you have a complicated query that last for several lines; you cannot use the command line to dump the result to a file easily. In such cases, you can put your complicated query into a file, such as longquery_file.sql, then execute the command.

    mysql -h hn -u un -ppwd < longquery_file.sql DBNAME > output.txt
    

    This worked for me. The only difficulty with me is the tab character; sometimes I use for group_cancat(foo SEPARATOR 0x09) will be written as '\t' in the output file. The 0x09 character is ASCII TAB. But this problem is not particular to the way we dump sql results to file. It may be related to my pager. Let me know when you find an answer to this problem. I will update this post.

    0 讨论(0)
  • 2020-11-29 01:08

    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');
    
    0 讨论(0)
  • 2020-11-29 01:14

    Use the below query:

     SELECT * FROM document INTO OUTFILE 'c:/order-1.csv' FIELDS TERMINATED BY ','  
     ENCLOSED BY '"' LINES TERMINATED BY '\n';
    
    0 讨论(0)
提交回复
热议问题