Export MySQL Data to CSV File

Just an example of using INTO OUTFILE for dumping sql data directly to a CSV file including column labels as the first row. Because I tend to forget these things over time, I’m posting this for future reference.

?View Code SQL
SELECT 'Firstname', 'Lastname', 'Email', 'Company', 'Title',
'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone1'
UNION
SELECT users.firstname, users.lastname, users.email, users.company, users.title,
users.address1, users.address2, users.city, users.state, users.zip, users.phone1
INTO OUTFILE '/tmp/dump_users.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY ''
LINES TERMINATED BY '\r\n'
FROM users;

This entry was posted on Tuesday, October 21st, 2008 at 9:49 am and is filed under Code. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

« Vintage Computer Pictorial
PHP cURL »

Comments are closed.

potatoeskillme
  • Stuff

    • Tioga Pass Eastern Yosemite
    • Mammoth Thanksgiving 2011
    • Road Trip to Monfuckintana
    • Silverton Road Trip 2011
    • Mammoth Thanksgiving 2010
    • Sequoia 2010
    • Big Bear Session
    • Deer Valley Spring Skiing
    • Jackson Hole Road Trip Redux
    • Mammoth Thanksgiving 09
    • NJ Surf Trip Sept 2009
    • Yosemite Aug 2009
    • Big Bear B-day Bash
    • Wedge 7.24.09
    • Morning Surf 7.25.09
    • Morning Surf 7.24.09
    • July 4th, 2009 – 41st Newport
    • Lake Don Pedro
    • Trails Highlights
    • Jackson Hole Road Trip
    • Building a Carputer