How to export CSV file with header in mysql

How to export CSV file with header in mysql

Refer to the official document http://dev.mysql.com/doc/refman/5.7/en/select-into.html

mysql> select game,domain,type

-> into outfile 'd:\\game.csv' 
-> fields terminated by ','

-> lines terminated by '\n'

-> from game_lists limit 10;

The following are examples:

mysql> create table test(id int(10) not null auto_increment primary key, name varchar(10) not null, age tinyint(2) not null)engine=innodb default charset=utf8;

mysql> insert into test(`name`,`age`) values ​​('Lee',20),('Li',30),('Wang',22),('Feng',23);

Check the results first

mysql> select * from (select 'name','age' union select name,age from test) b;
+------+-----+
| name | age |
+------+-----+
| name | age |
| Lee | 20 |
| Li | 30 |
| Wang | 22 |
| Feng | 23 |
+------+-----+
5 rows in set (0.00 sec)

Export CSV file

mysql> select * into outfile 'd:\\tmp\\columns.csv' fields terminated by ',' lines terminated by '\n' from (select 'name','age' union select name,age from test) b;
Query OK, 5 rows affected (0.00 sec)

The above method of exporting MySQL CSV files with headers is all I have 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 mysql query results to csv
  • 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

<<:  Alibaba Cloud Server Ubuntu Configuration Tutorial

>>:  Let’s take a look at JavaScript precompilation (summary)

Recommend

Detailed explanation of SELINUX working principle

1. Introduction The main value that SELinux bring...

JavaScript imitates Jingdong magnifying glass special effects

This article shares the specific code of JavaScri...

Simply learn various SQL joins

The SQL JOIN clause is used to join rows from two...

MySQL Billions of Data Import, Export and Migration Notes

I have been taking a lot of MySQL notes recently,...

mysql5.7.18.zip Installation-free version configuration tutorial (windows)

This is the installation tutorial of mysql5.7.18....

Tips for data statistics in MySQL

As a commonly used database, MySQL requires a lot...

A brief discussion on the correct posture of Tomcat memory configuration

1. Background Although I have read many blogs or ...

6 solutions to IDEA's inability to connect to the MySQL database

This article mainly introduces 6 solutions to the...

About front-end JavaScript ES6 details

Table of contents 1. Introduction 1.1 Babel Trans...

MySQL database rename fast and safe method (3 kinds)

Table of contents How to rename MySQL database Th...

Thumbnail hover effect implemented with CSS3

Achieve resultsImplementation Code html <heade...

HTML hyperlink style (four different states) setting example

Copy code The code is as follows: <style type=...