在将数据从MySQL表导出到文本文件时,我们也可以使用“ FIELDS TERMINATED BY”,“ ENCLOSED BY”,“ LINES TERMINATED BY”和其他选项将字段的值放在文本文件的不同设置中。可以通过以下示例进行说明-
假设我们从表'Student_info'获得以下数据-
mysql> Select * from Student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +------+---------+------------+------------+ 6 rows in set (0.07 sec)
假设我们只希望将上表中的两列“ id”和“ Name”导出到文件中,那么以下查询可以将“ Student_info”表中的“ id”和“ name”值仅导出到名为“ student3.txt'-
mysql> Select id, Name from Student_info INTO OUTFILE 'C:/mysql/bin/mysql-files/student3.txt' FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”’ LINES TERMINATED BY ‘\r\n’;
上面的查询将创建一个名为“ Student3.txt”的文件,并以不同的格式将“ Student_info”表中“ id”和“ name”列的值导出到该文件。
上面的查询将对Student3.txt中的数据进行格式化,如下所示:
"101","YashPal" "105","Gaurav" "125","Raman" "130","Ram" "132","Shyam" "133","Mohan"