1. 程式人生 > >轉載:MySQL INTO OUTFILE/INFILE匯出匯入資料

轉載:MySQL INTO OUTFILE/INFILE匯出匯入資料

--使用SELECT ... INTO OUTFILE 以逗號分隔欄位的方式將資料匯入到一個檔案中:

SELECT * INTO OUTFILE 'D:\\log1.txt' FIELDS TERMINATED BY ',' FROM log.log1

--將剛剛匯出的檔案log1.txt匯入到表log1相同結構的log2中:

LOAD DATA INFILE 'D:\\log1.txt' INTO TABLE aa.log2 FIELDS TERMINATED BY ','

-- 使用select * into outfile 匯出:

SELECT * INTO OUTFILE 'd:\\test.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM test.table

-- 匯入

LOAD DATA INFILE '/tmp/fi.txt' INTO TABLE test.fii FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

FIELDS TERMINATED BY ',' 欄位間分割符 OPTIONALLY ENCLOSED BY '"' 將欄位包圍 對數值型無效 LINES TERMINATED BY '\n' 換行符

Demo:

use authority; SELECT  '姓名','時間' UNION ALL SELECT `name`,creat_time from ryan_test  INTO OUTFILE 'E:\\ryan.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '''' LINES TERMINATED BY '\r\n';