如何根据 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 ','; Query OK, 4 rows affected (0.16 sec)
以上查询将把以下值导出到 Student4.CSV 文件中:-
125 Raman Shimla Computers 130 Ram Jhansi Computers 132 Shyam Chandigarh Economics 133 Mohan Delhi Computers
广告