2

i use this query but it not show the column name

SELECT * FROM employeemaster INTO OUTFILE 'c:/order-1.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

2

2 Answers 2

3

Use the below query:

SELECT empid as employeeid,sname as staffname FROM employeemaster INTO
OUTFILE 'c:/order.csv' FIELDS 
TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Sign up to request clarification or add additional context in comments.

Comments

1

There are two possible solutions:

The simple one : hardcode the colum names yourself and join the header line with the query result :

SELECT 'Name_I_Want_For_Col1', 'Name_I_Want_For_Col1', 'Name_I_Want_For_Col1'
UNION ALL
SELECT ColName1, ColName2, ColName3
    FROM YourTable
    INTO OUTFILE '/path/outfile'

A more straight forward (but more complex one) : Get the colum names from information schema :

select GROUP_CONCAT(CONCAT("'",COLUMN_NAME,"'"))
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'my_table'
AND TABLE_SCHEMA = 'my_schema'
order BY ORDINAL_POSITION
UNION ALL
    SELECT ColName1, ColName2, ColName3
        FROM YourTable
        INTO OUTFILE '/path/outfile'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.