0

I have mysql script which select needed data via LEFT JOIN and save result in csv file. But the problem is column names are missed in result file. What i doing wrong ?

SELECT 
a.*, b.pr_name
INTO OUTFILE 'johnoggyresult3.csv'
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
FROM
    `fileb` AS b
LEFT JOIN
    `filea` AS a ON b.pr_hand_size = a.user_hand_size
ORDER BY
     a.user_id
John
  • 31
  • 3

1 Answers1

0

Just add columns names with SELECT. Something like this should work:

SELECT * 

INTO OUTFILE 'johnoggyresult3.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

FROM (
SELECT 'column1', 'column2'
UNION ALL
(SELECT 
a.column1, b.pr_name
FROM
    `fileb` AS b
LEFT JOIN
    `filea` AS a ON b.pr_hand_size = a.user_hand_size
ORDER BY
     a.user_id) AS q
)

Please note that dumping to CSV is done at the end, after result was fetched and sorted and after headers were attached to dataset. You also need to sort data first before adding row with headers or otherwise headers would be sorted too and could land somewhere in the middle of dataset instead of top like intended.

Perfect Square
  • 1,368
  • 16
  • 27
  • sorry i didnt understood your answer...what is column1 and column2 in your example ? Could you please make an example according to my code. Thank you for answer ! – John Jul 25 '17 at 18:05
  • I don't know what columns are in your database (hidden behind * sign so I used 'column1', 'column2' as an example. You have to explicitly list columns names returned by " a.* ". Do not use asterisk. – Perfect Square Jul 25 '17 at 20:50