2

MySQL has the functionality to export the data to a CSV file with following statement

SELECT 
    *
FROM
    person
INTO OUTFILE 'person.csv' 
FIELDS ENCLOSED BY '"' 
TERMINATED BY ',' 
ESCAPED BY '"' 
LINES TERMINATED BY '\r\n';

But by default it will has the column name as the CSV header, so how to remove the header in this statement?

I reviewed the mysql reference but seems these is no such info.

Liping Huang
  • 4,378
  • 4
  • 29
  • 46

3 Answers3

1

2 options are provided in this post : How can I suppress column header output for a single SQL statement?)

1 : invoke mysql with -N flag will skip all column headers

2 : fake it

select column1 as '', column2 as '' from some_table;
1

After complete the csv file, seems for the huge records, the big csv file has no header, so if you want to add the header, following:

Several users asked about including headers, i.e., column names or variable names, in the "INTO OUTFILE" syntax.

One approach is to use the "--column-names" option in the mysql invocation:

mysql --column-names -e 'SELECT * FROM mysql.user' > test.dat

(This creates a tab-delimited file test.dat with column names in the first row followed by the query results.)

Community
  • 1
  • 1
Liping Huang
  • 4,378
  • 4
  • 29
  • 46
-1
SELECT 
    *
FROM
    person
INTO OUTFILE 'person.csv' 
FIELDS ENCLOSED BY '"' 
TERMINATED BY ',' 
ESCAPED BY '"' 
LINES TERMINATED BY '\r\n';

change FIELDS ENCLOSED BY '"' line
with OPTIONALLY ENCLOSED BY '"'
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80