Database

Tutorial How to ADD Column Header in MySQL CSV Outfile

As you know from previous article, standard query used to export mysql database to an excel csv file has no column header inside.

To put column header in your exported csv file you can simply modify your query as follow:

SET @sql_text =
CONCAT (“
SELECT  ‘Name’,’Department’,’Age’
UNION ALL
SELECT name,department,age
FROM t_orang
INTO OUTFILE ‘C:/myfile.csv’
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘\n’
“);
PREPARE s1 FROM @sql_text;
EXECUTE s1;
DROP PREPARE s1;

It works!, right?…

Did You notice above code?, to add column header in exported csv file you can combine standard query with UNION ALL function and bundle it as a variable named sql_text.

*) Third line is used to declare the header column name, edit it as you wish.

Leave a Reply

1 Comment

[…] Anyway, did you notice inside your .csv file there is no column header?,  to make your exported csv file is human readable it is better to put column header inside, read the tutorial here. […]