Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構

6.9. OUTFILE/LOAD DATA INFILE

查詢結果輸出到檔案

SELECT * FROM tablename INTO OUTFILE '/tmp/tablename.txt';
		

使用tee將屏幕輸出到檔案

		
mysql>tee /home/neo/screen.txt
mysql>select * from user;
mysql>exit
		
		
		
SELECT * INTO OUTFILE '/home/mark/Orders.txt'
  FIELDS
  TERMINATED BY = ','
  FROM Orders
  WHERE Order_Date >= '2000-01-01'
		
		
		
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
		
		

6.9.1. Export data to CSV from MySQL

SELECT *
INTO OUTFILE '/tmp/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM products
			

6.9.2. Import data from CSV file.

LOAD DATA LOW_PRIORITY LOCAL INFILE 'C:\\hx.csv' IGNORE INTO TABLE `tmp`.`creditlog`
CHARACTER SET gbk FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n'
(`ctime`, `login`, `mode`, `type`, `prevavailcredit`, `change`, `newavailcredit`, `comment`);