1、導出數據(不指定分割符)
mysql> select * from class_info into outfile '/tmp/class_info.csv';
mysql> system cat /tmp/class_info.csv
1 1601 a 87
2 1601 b 90
3 1602 d 91
4 1602 c 85
5 1603 e 88
-------------------------------
2、導出數據,欄位分隔符為",",欄位引用符為「 " 」(雙引號)
mysql> select * from class_info into outfile '/tmp/class_info_1.csv' fields terminated by "," enclosed by '"';
Query OK, 5 rows affected (0.00 sec)
mysql> system cat /tmp/class_info_1.csv
"1","1601","a","87"
"2","1601","b","90"
"3","1602","d","91"
"4","1602","c","85"
"5","1603","e","88"
3、導出數據,欄位分隔符為",",數值型欄位不加引用符,其餘欄位加引號
mysql> select * from class_info into outfile '/tmp/class_info_2.csv' fields terminated by "," optionally enclosed by '"';
Query OK, 5 rows affected (0.02 sec)
mysql> system cat /tmp/class_info_2.csv
1,1601,"a",87
2,1601,"b",90
3,1602,"d",91
4,1602,"c",85
5,1603,"e",88
4、數據恢復
使用load infile恢復
備份如第1種情況,恢復如下
mysql> load data infile '/tmp/class_info.csv' into table class_info;
備份如第2種情況,恢復如下
mysql> load data infile '/tmp/class_info_1.csv' into table class_info fields terminated by "," enclosed by '"';
備份如第3中情況,恢復如下
mysql> load data infile '/tmp/class_info_2.csv' into table class_info fields terminated by "," optionally enclosed by '"';
使用mysqlimport恢復
# mysqlimport -uroot -p tws /tmp/class_info.csv
Enter password:
tws.class_info: Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
查看結果
# mysql -uroot -p -e "select * from tws.class_info"
當然,如果條件允許,能使用navicat 或者workbench 等工具獲取MySQL表數據將更加方便。