Jump to content

OUTFILE


Asheeown

Recommended Posts

I'm exporting results from a query into a CSV file this is what I have so far

 

 

SELECT id,name,job,address
    FROM users
    INTO OUTFILE '/tmp/users.csv'
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\n' 

 

 

Now I want the first line of that csv file to be

User ID,Name,Job,Address

of course they would be in different fields since it's comma separated

Link to comment
Share on other sites

You can use a UNION to achieve this (if using MySQL 4.0 or higher)

 


SELECT 'User ID','Name','Job','Address'
UNION
SELECT id,name,job,address
    FROM users
    INTO OUTFILE '/tmp/users.csv'
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\n' 

 

 

Link to comment
Share on other sites

You can use a UNION to achieve this (if using MySQL 4.0 or higher)

 


SELECT 'User ID','Name','Job','Address'
UNION
SELECT id,name,job,address
    FROM users
    INTO OUTFILE '/tmp/users.csv'
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\n' 

 

 

 

Interesting... I assume this actually works? I didn't realize that it would "guess" properly.

Link to comment
Share on other sites

It does ... if your default character sets and collations are the same as your table - you may need to force the character /collation on the string literals (see below to force latin1/latin1_swedish_ci )

 

SELECT _latin1 'User ID' collate latin1_swedish_ci, _latin1 'Name' collate latin1_swedish_ci ....
UNION
SELECT id,Name ....

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.