简体   繁体   中英

How to export table to excel from mysql database?

I have a table in mysql which is quite big for having over 100k rows and I want to export it to excel. However, I tried the export to excel function on phpmyadmin but it takes forever to dump the excel file. It's not even dumping. The error is always, "the connection is reset". Is there an alternative way on how to do this??

First, 100k rows in Excel sounds like a horrible idea and of course it'll take awhile. This is going to take awhile just to open. If you MUST do this try:

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

This should give you a file called: /tmp/orders.csv which will open in Excel.

The slowness of your export is likely due to the server on which phpmyadmin runs. I regularly export millions of rows with surprising speed.

If you have access to the file system of your database server, this is a very fast way of converting to .csv, which in turn can be opened by Excel.

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

See this: http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/

I've used the OUTFILE method before to do this:

SELECT
    'First Name',
    'Last Name',
    'Email Address'
UNION
SELECT
    First_Name,
    Last_Name,
    Email
INTO OUTFILE
    '/tmp/data_2012-05-03.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
FROM
    `data`
WHERE
    Reg_Date >= '2012-05-02 00:00:00' AND
    Reg_Date <= '2012-05-02 23:59:59' AND
    Email_Status = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM