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

Detailed explanation of Vite's new experience

What is Vite? (It’s a new toy on the front end) V...

Implementation steps for installing FTP server in Ubuntu 14.04

Table of contents Install Software Management Ano...

MySQL-8.0.26 Configuration Graphics Tutorial

Preface: Recently, the company project changed th...

How to implement insert if none and update if yes in MySql

summary In some scenarios, there may be such a re...

How to deploy Confluence and jira-software in Docker

version: centos==7.2 jdk==1.8 confluence==6.15.4 ...

Will CSS3 really replace SCSS?

When it comes to styling our web pages, we have t...

Concat() of combined fields in MySQL

Table of contents 1. Introduction 2. Main text 2....

How to build a redis cluster using docker

Table of contents 1. Create a redis docker base i...

Solution to the problem of mysql service starting but not connecting

The mysql service is started, but the connection ...

Tips on disabling IE8 and IE9's compatibility view mode using HTML

Starting from IE 8, IE added a compatibility mode,...