2

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/

Sign up to request clarification or add additional context in comments.

2 Comments

When I replace [tablename] with my table, this query returns an empty set for my table..? Google is not my friend.
Did you enclose your tablename in ticks? Like this: FROM `tablename`
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 Comments

If you read my question you'd see I am using phpMyAdmin indeed and I have ALREADY done an export from that tab.
Yes. You're missing a key step of enclosing the fields.
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..
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.
How can an outfile be accessed through phpMyAdmin? Though it returned an empty set, maybe the file is still there with populated data.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.