Search code examples
mysqlubuntumysql-workbenchinto-outfile

INTO OUTFILE concatenating columns


I am running the below query in MYSQL:

SELECT 'id','uuid','progid','progname','priority','proglist' UNION SELECT programtbl.id,programtbl.uuid,programtbl.progid,programtbl.progname,programtbl.priority,programtbl.proglist INTO OUTFILE '/tmp/data22.csv' FROM programtbl;

I am running this query inside MYSQL shell in Ubuntu terminal. The query runs successfully and the file is also generated correctly. However the records appear all concatenated. They dont appear separated per column. My current output:

enter image description here

My current output above is concatenating the columns. My expected output should be as below:

enter image description here

Not sure what is wrong with my query. Can someone please help fix this output?


Solution

  • You are not specifying the comma delimited formatting.

    SELECT 'id','uuid','progid','progname','priority','proglist' 
    UNION 
    SELECT programtbl.id,programtbl.uuid,programtbl.progid,programtbl.progname,programtbl.priority,programtbl.proglist 
    INTO OUTFILE '/tmp/data22.csv' 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    FROM programtbl;