Home > Database > Mysql Tutorial > How can we import only specific columns from a text file into a MySQL table?

How can we import only specific columns from a text file into a MySQL table?

王林
Release: 2023-09-22 13:13:02
forward
623 people have browsed it

我们如何才能仅将文本文件中的特定列导入到 MySQL 表中?

Suppose if we have the values ​​of some particular column in a text file and the MySQL table into which we are importing data has an extra column, then by mentioning the column in the query name, we can upload only the values ​​of these specific columns. It can be understood by the following example -

Example

Suppose we have only the values ​​of "id", "Name" and "Salary" columns in the text file as follows-

105,Chum,11000
106,Danny,12000
Copy after login

Now, while importing this text file into MySQL table, we need to mention the names of the columns that have values ​​in the text file in the query as shown below -

mysql> LOAD DATA LOCAL INFILE 'd:\A.txt' INTO table employee9_tbl FIELDS TERMINATED BY ','(id, Name, Salary);
Query OK, 2 rows affected (0.04 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0

mysql> Select * from employee9_tbl;
+------+----------------+----------+--------+
| Id   | Name           | Country  | Salary |
+------+----------------+----------+--------+
| 105  | Chum           | NULL     | 11000  |
| 106  | Danny          | NULL     | 12000  |
+------+----------------+----------+--------+
2 rows in set (0.00 sec)
Copy after login

As can be seen from the above result set , MySQL only uploads the values ​​​​of three columns, namely Id, Name and Salary. It stores NULL in the "Country" field.

The above is the detailed content of How can we import only specific columns from a text file into a MySQL table?. 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