How can I export values based on some conditions from MySQL table into a file?


We can use the conditions in WHERE clause while exporting the data from MySQL table to a file. It can be understood with the help of an 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 to export the records having a value of id more than 120 then the following query will export such kind of records from ‘Student_info’ table into ‘Stuednt4.CSV’ file −

mysql> Select * from student_info WHERE id > 120 into outfile 'C:/mysql/bin/mysql-files/student4.csv' Fields terminated by ',';
Query OK, 4 rows affected (0.16 sec)

The query above will export the following values into Student4.CSV file −

125   Raman    Shimla      Computers
130   Ram      Jhansi      Computers
132   Shyam    Chandigarh  Economics
133   Mohan    Delhi       Computers

Updated on: 06-Feb-2020

319 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements