如何将基于某些条件的值从 MySQL 表导出到文件中?

如何将基于某些条件的值从 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

登录后复制

以上就是如何将基于某些条件的值从 MySQL 表导出到文件中?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

上一篇 如何在 MySQL INSERT 语句中指定默认值?
下一篇 如何重复存储在MySQL表的数据列中的值?