
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Settings for Exporting MySQL Table Values to Text File
While exporting the data from MySQL table to a text file we can use ‘FIELDS TERMINATED BY’, ‘ENCLOSED BY’, ‘LINES TERMINATED BY’ and other options too to put the values of fields in different settings of the text file. It can be illustrated with the help of the following example −
Example
Suppose we are having following data from table ‘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)
Suppose we want only two columns ‘id’ and ‘Name’ from the above table to be exported into a file then the following query can export the values of only ‘id’ and ‘name’ from ‘Student_info’ table into a file named ‘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
’; Query OK, 6 rows affected (0.07 sec)
The above query will create a file named ‘Student3.txt’ and export the values of columns ‘id’ and ‘name’ from ‘Student_info’ table to it in different formatting.
The above query will do the formatting of data in Student3.txt as follows −
"101","YashPal" "105","Gaurav" "125","Raman" "130","Ram" "132","Shyam" "133","Mohan"