How to export mysql query results to csv

How to export mysql query results to csv

To export MySQL query results to csv , you usually use php to connect to mysql to execute the query, use php to generate the returned query results in csv format and then export them.

But this is more troublesome and requires PHP to be installed on the server.

Directly use mysql to export csv method

We can use into outfile, fields terminated by, optionally enclosed by, line terminated by statements to export csv

Statement format and function

into outfile 'Exported directory and file name'
Specify the export directory and file name

fields terminated by 'field separator'
Defines the separator between fields

optionally enclosed by 'Field Encloser'
Defines the characters that enclose the field (not valid for numeric fields)

lines terminated by 'line separator'
Defines the separator for each line

example:

mysql -u root
use test;
select * from table into outfile '/tmp/table.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';

After execution, the recorded data in talbe will be exported to the /tmp/table.csv file. Each field is separated by , and the field content is a string surrounded by ", and each record uses \r\n for line breaks.

The above method of exporting MySQL query results to csv is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to solve the problem of Chinese garbled characters when importing and exporting csv in Mysql
  • How to import csv format data file solution into MySQL
  • How to export MySQL data to csv format
  • PHP exports MySQL data to Excel file (fputcsv)
  • How to solve the problem of unsuccessful import of csv data into mysql using SQLyog
  • How to import csv file into mysql database using php
  • How to export CSV file with header in mysql
  • Python implements the method of exporting data from MySQL database table to generate csv format file
  • How to parse csv data and import it into mysql
  • Import csv file into mysql using navicat

<<:  How to test the maximum number of TCP connections in Linux

>>:  Vue implements sample code for dragging files from desktop to web page (can display pictures/audio/video)

Recommend

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

CSS to achieve single-select folding menu function

Don’t introduce a front-end UI framework unless i...

ReactHooks batch update state and get route parameters example analysis

Table of contents 1. How to update in batches Con...

Detailed explanation of Bind mounts for Docker data storage

Before reading this article, I hope you have a pr...

Adobe Brackets simple use graphic tutorial

Adobe Brackets is an open source, simple and powe...

Gitlab practical tutorial uses git config for related configuration operations

This article introduces the content related to gi...

Use scripts to package and upload Docker images with one click

The author has been working on a micro-frontend p...

IE6 BUG and fix is ​​a preventive strategy

Original article: Ultimate IE6 Cheatsheet: How To...

Detailed explanation of JS homology strategy and CSRF

Table of contents Overview Same Origin Policy (SO...

JavaScript to show and hide the drop-down menu

This article shares the specific code for JavaScr...

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nic...

Detailed instructions for installing Jenkins on Ubuntu 16.04

1. Prerequisites JDK has been installed echo $PAT...

Implementation of Docker cross-host network (overlay)

1. Docker cross-host communication Docker cross-h...

How to display small icons in the browser title bar of HTML webpage

Just like this effect, the method is also very si...