MySQL SELECT to a CSV file

Lets use the following “SELECT” statement as an example

SELECT id,product_name,qty FROM orders

which returns three columns of data, the results can be placed into the file /orders/order_list.csv using the query:

SELECT id,product_name,qty
FROM orders
INTO OUTFILE '/orders/order_list.csv'

The result will be a tab-separated file, each row a new line. You can change the output behavior by doing the following:

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/orders/order_list.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

This will result in the following output

"1","Tech-Recipes sock puppet","14.95"
"2","Tech-Recipes chef's hat","18.95"

NOTE:

Make sure the output file does not exit and that the mysql user have write permissions

 
/home/dodgydevil/wiki.zendfusion.com/data/pages/scripts/mysql/mysql_select_to_csv_file.txt · Last modified: 2010/09/30 02:05 by bosbaba
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
Zend Fusion