Asheeown Posted April 8, 2007 Share Posted April 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/46188-outfile/ Share on other sites More sharing options...
fenway Posted April 9, 2007 Share Posted April 9, 2007 I've never actually done this with MySQL... not sure if outfile even has such an option. Quote Link to comment https://forums.phpfreaks.com/topic/46188-outfile/#findComment-224868 Share on other sites More sharing options...
Tyche Posted April 9, 2007 Share Posted April 9, 2007 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' Quote Link to comment https://forums.phpfreaks.com/topic/46188-outfile/#findComment-224970 Share on other sites More sharing options...
fenway Posted April 9, 2007 Share Posted April 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/46188-outfile/#findComment-224994 Share on other sites More sharing options...
Tyche Posted April 9, 2007 Share Posted April 9, 2007 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 .... Quote Link to comment https://forums.phpfreaks.com/topic/46188-outfile/#findComment-225005 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.