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.
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; |