How can we export some field(s) from MySQL table into a CSV file?

MySQLMySQLi Database

It can be done by providing the column(s) names in the SELECT … INTO OUTFILE statement while exporting the data from MySQL table into a file. We are illustrating it 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 ‘student1.csv’ −

mysql> Select id, Name from Student_info INTO OUTFILE 'C:/mysql/bin/mysql-files/student1.csv';
Query OK, 6 rows affected (0.07 sec)

The above query will create a file named ‘Student1.csv’ and export the values of columns ‘id’ and ‘name’ from ‘Student_info’ table into it.

raja
Published on 20-Feb-2018 12:54:19
Advertisements