export data from mysql to csv code example

Example 1: mysql export query result to csv

SELECT order_id,product_name,qty
FROM orders
WHERE foo = 'bar'
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Example 2: how to dump .csv file into mysql

LOAD DATA LOCAL INFILE 'c:/country.csv' 
INTO TABLE country 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

Example 3: export mysql table to file

mysqldump db_name tbl_name;

Tags:

Sql Example