LOADDATALOCALINFILE'd:\"/> LOADDATALOCALINFILE'd:\">

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?

王林
王林forward
2023-09-22 13:13:02643browse

我们如何才能仅将文本文件中的特定列导入到 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

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)

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete