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

Summary of 4 methods of div+css layout to achieve 2-end alignment of css

The div+css layout to achieve 2-end alignment is ...

Detailed tutorial for downloading and installing mysql8.0.21

Official website address: https://www.mysql.com/ ...

HTML (css style specification) must read

CSS style specifications 1. Class Selector 2. Tag...

A simple way to call desktop exe programs on a web page

This article mainly introduces how to call desktop...

Tutorial on installing AutoFs mount service under Linux

Whether it is Samba service or NFS service, the m...

jQuery realizes the scrolling effect of table row data

This article example shares the specific code of ...

Vue implements the magnifying glass effect of tab switching

This article example shares the specific code of ...

Analysis of three parameters of MySQL replication problem

Table of contents 01 sql_slave_skip_counter param...

Detailed introduction to CSS font, text, and list properties

1. Font properties color, specifies the color of ...

How to use Nginx to prevent IP addresses from being maliciously resolved

Purpose of using Nginx Using Alibaba Cloud ECS cl...

How to use & and nohup in the background of Linux

When we work in a terminal or console, we may not...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

Disable input text box input implementation properties

Today I want to summarize several very useful HTML...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

How to use jconsole to monitor remote Tomcat services

What is JConsole JConsole was introduced in Java ...