Home > Database > Mysql Tutorial > body text

How to export values ​​based on certain criteria from a MySQL table to a file?

WBOY
Release: 2023-09-15 20:17:05
forward
810 people have browsed it

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

We can use conditions in the WHERE clause while exporting data from a MySQL table to a file. It can be understood through examples -

Example

Suppose we have the following data from the 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)
Copy after login

Suppose we want to export records with id values ​​greater than 120, then The following query will export such records from the "Student_info" table into the "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)
Copy after login

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

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

The above is the detailed content of How to export values ​​based on certain criteria from a MySQL table to a file?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!