Sunday, June 17, 2007

Upload results of a SQl to a CSV file

1) Method 1 - in the shell

mysql -B -u -p -e 'SELECT foo FROM bar ' | \

perl -F"\t" -lane 'print join ",", map {s/"/""/g; /^[\d.]+$/ ? $_ : qq("$_")} @F ' >> output.csv

2) Method 2 - in mysql promp
SELECT * from Dealers INTO OUTFILE 'orders.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

No comments: