MySQL export data in csv format _ MySQL

Source: Internet
Author: User
This article describes how to export MySQL data in csv format. you can refer to the following solutions for more information:

1. into outfile

The code is as follows:


SELECT * FROM mytable
Into outfile '/tmp/mytable.csv'
Fields terminated ','
Optionally enclosed '"'
Lines terminated by '\ n ';

I found a particularly serious problem during usage. I cannot insert query conditions, such as where, that is, I can only export the entire table. I don't know if there is a problem with my writing, if you have any idea, please leave a message for me.

The second problem is that the outfile path must have the write permission. the mysql process permission is generally mysql user, so it is best to export it to the/tmp directory.

2. combine sed

The code is as follows:


Mysql-uroot test-e "select ip from server where. name like '% abc %' "-N-s | sed-e's/^/"/g; s/$/"\ n/g';> /tmp/test.csv

Here, we first use the-e parameter of the mysql command to execute the SQL statement, then use-N to remove the column name in the output result, and-s to remove various dashes in the output result.

Then, use the sed command to replace all the relevant data in the output result, with three replicas, 1. add ", add at the end of a row" and line feed at the beginning of the row, and add "," to separate each field.

3. implemented through mysqldump

The code is as follows:


Mysqldump-u username-p-t-T/path/to/directory dbname table_name -- fields-terminated-by = ','

Similar to solution 1.

Take a note.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.