Export MySQL query results to CSV

Source: Internet
Author: User

To export MySQL query results as CSV, you will typically use PHP to connect to MySQL to execute the query, and then export the returned query results using PHP to generate CSV format.

But this is more troublesome, requires the server to install PHP can be implemented.

export CSV method directly using MySQL

We can use the into OutFile, the fields terminated by, the optionally enclosed by, the line terminated by statement to implement the export CSV

the format and function of the statement

into outfile ' exported directory and filename '
Specify the exported directory and file name

Fields terminated by ' Inter-field separator '
To define separators between fields

Optionally enclosed by ' field bracketing '
Define the character that surrounds the field (invalid numeric field)

Lines terminated by ' inline separators '
Define delimiters for each row

Example:
Mysql-u root use test;select  * Span style= "color: #000088; Box-sizing:border-box; " >from  table  into  outfile  '/tmp/table.csv '  fields Terminated by   ', '  optionally enclosed by   lines terminated  ' \ r \ n ' ; 
  
 
    • 1
    • 2
    • 3

After execution, the recorded data in the Talbe is exported to the/tmp/table.csv file. Each field is delimited by, and the contents of the field are "surrounded by strings, with each record using \ r \ n line-wrapping."

Export MySQL query results to CSV

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.