11

I want export csv directly from mysql with command

SELECT .... 
FROM ...
INTO OUTFILE '/tmp/export.csv' 
FIELDS TERMINATED BY ',' 
ESCAPED BY '\\'
LINES TERMINATED BY '\n' ;

This work perfectly, but encode not is utf8.How make the content exported utf8 encoding?

1

1 Answer 1

23

As documented under SELECT ... INTO Syntax:

SELECT ... INTO OUTFILE is the complement of LOAD DATA INFILE. Column values are written converted to the character set specified in the CHARACTER SET clause. If no such clause is present, values are dumped using the binary character set. In effect, there is no character set conversion. If a result set contains columns in several character sets, the output data file will as well and you may not be able to reload the file correctly.

The grammar is documented under SELECT Syntax:

    [INTO OUTFILE 'file_name'
      [CHARACTER SET charset_name]

Therefore:

SELECT .... 
FROM ...
INTO OUTFILE '/tmp/export.csv'
CHARACTER SET utf8 
FIELDS TERMINATED BY ',' 
ESCAPED BY '\\'
LINES TERMINATED BY '\n' ;
Sign up to request clarification or add additional context in comments.

6 Comments

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8
@eggyal the point is that there seems to be a syntax error issue with what you wrote because the utf8 option isn't coming up in phpmyadmin i.imgur.com/jYKZqjI.png
For windows user, write the path in the following way: c:/temp/export.csv
Also note that MySQL's "utf8" charset doesn't handle 4 bytes unicode characters. If your utf8 export causes errors with some characters, use CHARACTER SET utf8mb4 instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.