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.

« MyTV
PHP cURL »

Comments are closed.

potatoeskillme
  • Stuff

    • Building a Carputer
    • Jackson Hole Road Trip
    • Trails Highlights
    • Lake Don Pedro
    • July 4th, 2009 – 41st Newport
    • Morning Surf 7.24.09
    • Wedge 7.24.09
    • Morning Surf 7.25.09
    • Big Bear B-day Bash
    • Yosemite Aug 2009
    • NJ Surf Trip Sept 2009
    • Mammoth Thanksgiving 09
    • Jackson Hole Road Trip Redux
    • Photos
  • Recent Posts

    • TI-86 Intervalometer for Canon XTi
    • Bad interpreter error – linux shell script
    • MintTv
    • PHP cURL
    • Export MySQL Data to CSV File
  • Categories

    • Code
    • Older Projects
    • Uncategorized
  • Links

    •  Kyle Cummings Resume
    • Alexei Udarbe
    • Evan Wise
    • John Rivas
    • Mary Redaelli