如何根据某些条件将值从MySQL表导出到文件中?
在将数据从MySQL表导出到文件时,我们可以使用WHERE子句中的条件。可以借助示例来理解-
示例
假设我们从表'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值大于120的记录,则以下查询将从“Student_info”表中将此类记录导出到“Stuednt4.CSV”文件中-
mysql> Select * from student_info WHERE id > 120 into outfile 'C:/mysql/bin/mysql-files/student4.csv' Fields terminated by ',';
上面的查询将以下值导出到Student4.CSV文件中-
125 Raman Shimla Computers 130 Ram Jhansi Computers 132 Shyam Chandigarh Economics 133 Mohan Delhi Computers