MySQL - export to csv some columns with quotes and some without; OPTIONALLY ENCLOSED BY not working correctly -


i exporting mysql table , want export integer type columns without double quotes varchar type columns double quotes. need have correct formatting next step in work. can done in mysql? know in python script csv files pretty large (>1 gb) think might take awhile that. anyway, possible using mysql queries?

here's current export script format:

select     'column_name_1',    'column_name_2',    ...    'column_name_n' union select * table outfile 'table.csv' fields terminated ',' lines terminated '\n'; 

if helps, here table (more importantly, types involved) trying export:

+-------------------------+------------------+------+-----+---------+-------+ | field                   | type             | null | key | default | | +-------------------------+------------------+------+-----+---------+-------+ | field_1                 | int(10) unsigned | no   | mul | 0       |       | | field_2                 | int(10) unsigned | no   | mul | null    |       | | field_3                 | int(10) unsigned | no   |     | null    |       | | field_4                 | char(1)          | no   |     | null    |       | | field_5                 | int(10) unsigned | no   |     | null    |       | | field_6                 | varchar(4)       | no   |     |         |       | | field_7                 | char(1)          | no   |     | y       |       | | field_8                 | varchar(20)      | no   |     |         |       | | field_9                 | varchar(200)     | no   |     |         |       | +-------------------------+------------------+------+-----+---------+-------+ 

edit 1: tried optionally enclosed '"' suggested in answer when add script, double quotes every column, not string (or varchar) columns. idea why might this?

use optionally enclosed by clause.

select * table outfile 'table.csv' fields terminated ','     optionally enclosed '"' lines terminated '\n'; 

the optionally modifier makes string columns.

you need leave out subquery returns header line. problem rows of union need have same types in columns, it's converting non-strings strings match header line.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -