MySQL自己提供了導入導出數據庫的工具,但有時我們需要僅僅導入導出單個表的數據,比如導入導出CSV文件,此時可以使用MySQL自動的命令來做導入導出工作。
導出語法如下:
SELECT * FROM [TABLE]
INTO OUTFILE '[FILE]';
或者
SELECT * FROM [TABLE]
INTO OUTFILE '[FILE]'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
下邊是一個具體的導出的例子:
SELECT * FROM mytable
INTO OUTFILE '/tmp/mytable.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
導入語法如下:
LOAD DATA INFILE '[FILE]'
INTO TABLE [TABLE];
或者
LOAD DATA INFILE '[FILE]'
INTO TABLE [TABLE]
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
下邊是一個具體的導入的例子:
LOAD DATA INFILE '/tmp/mytable.csv'
INTO TABLE mytable
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
摘自 kongxx的專欄