Export mysql table in CSV

5

I am trying to export a table from mysql to csv. This I have already achieved but in the csv file the fields of the exported columns do not appear.

The code I have used is the following:

select Nombre,Direccion INTO OUTFILE 'prueba.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n';

How can I also export the names of the columns?

    
asked by adamista 08.06.2016 в 22:47
source

2 answers

3

In your query you use FIELDS TERMINATED BY ';'

select Nombre, Direccion INTO OUTFILE 'prueba.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n';

If you want to create a .CSV file by definition you should mainly use FIELDS TERMINATED BY ','

the query would look like this:

(SELECT 'Nombre', 'Direccion') 
UNION ALL
(SELECT Nombre, Direccion INTO OUTFILE 'prueba.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n');
    
answered by 08.06.2016 / 23:01
source
3

To obtain the names of the columns in your csv you must use UNION.

Try trying the following:

(SELECT 'Nombre','Direccion')
UNION 
(SELECT Nombre,Direccion
FROM tu_tabla
INTO OUTFILE '/tmp/orders.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\r\n');
    
answered by 08.06.2016 в 23:01