1

I have a large MySQL table (8-9k records, I'm using phpMyAdmin) with some values that are descriptions, with commas.

I need to Export the SQL table into CSV form to use on a new platform and I can't for the life of me solve this issue:

When there are commas in certain fields, the data gets shoved to the next column and things get out of order.

I JUST NEED the EXACT table in MySQL to become a CSV. Any tips, ideas?

Thanks.

3 Answers 3

6

Use this query:

SELECT * INTO OUTFILE 'filename.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM [tablename]

Source: http://www.wausita.com/2011/02/mysql-export-csv/

2
  • When I replace [tablename] with my table, this query returns an empty set for my table..? Google is not my friend.
    – khaverim
    Aug 8, 2012 at 15:32
  • Did you enclose your tablename in ticks? Like this: FROM `tablename`
    – Lil' Bits
    Aug 8, 2012 at 15:56
4

Simply go to the database's Export tab. Be sure to enclose your fields. This way values are wrapped in " to avoid erroneous commas.

Otherwise, you can use mysqldump from the command line.

Or you can export data with a query:

SELECT *
FROM table
INTO OUTFILE '/tmp/table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
6
  • If you read my question you'd see I am using phpMyAdmin indeed and I have ALREADY done an export from that tab.
    – khaverim
    Aug 8, 2012 at 15:23
  • Yes. You're missing a key step of enclosing the fields. Aug 8, 2012 at 15:26
  • 3
    I have already used " to enclose the fields; Some descriptions also have " and thus the same error happens. This query returns an empty set for my table..
    – khaverim
    Aug 8, 2012 at 15:31
  • We are not going to be able to provide you with exact code. Ultimately your problem lies in escaping and enclosing. One of these answers should put you on the right path. But you're going to need to do some work yourself. Aug 8, 2012 at 15:42
  • How can an outfile be accessed through phpMyAdmin? Though it returned an empty set, maybe the file is still there with populated data.
    – khaverim
    Aug 8, 2012 at 15:42
2

My solution was exporting as an .xlsx from MySQL. The columns were rendered perfectly. Using several combinations of enclosing/escaping character was to no avail.

I then saved the .xlsx as a .csv and it was fine.

Thanks for your time guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.