Home > Database > Mysql Tutorial > How can we export certain fields from a MySQL table into a text file?

How can we export certain fields from a MySQL table into a text file?

WBOY
Release: 2023-09-09 11:09:10
forward
851 people have browsed it

我们如何将 MySQL 表中的某些字段导出到文本文件中?

This can be done by providing the column names in the SELECT … INTO OUTFILE statement when exporting data from a MySQL table to a file. We illustrate with the help of the following example-

Example

Suppose we get 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 only want to get the "id" from the above table " and "Name" columns are exported to the file, then the following query can export only the "id" and "name" values ​​in the "Student_info" table to a file named "student1.txt"-

mysql> Select id, Name from Student_info INTO OUTFILE 'C:/mysql/bin/mysql-files/student1.txt';
Query OK, 6 rows affected (0.07 sec)
Copy after login

The above query will create a file named "Student.txt" and export the values ​​of the "id" and "name" columns in the "Student_info" table to the file.

The above is the detailed content of How can we export certain fields from a MySQL table into a text 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