Home > Database > Mysql Tutorial > body text

How can we import data from a text file into a MySQL table using MySQL LOAD DATA INFILE statement with 'FIELDS TERMINATED BY' option?

WBOY
Release: 2023-09-02 11:13:06
forward
1377 people have browsed it

我们如何使用带有“FIELDS TERMINATED BY”选项的 MySQL LOAD DATA INFILE 语句将数据从文本文件导入到 MySQL 表中?

When we want to import a text file into a MySQL table with values ​​separated by comma (,) or any other delimiter like colon (:), we should use "FIELDS TERMINATED BY" ” option, can be understood through the following example -

Example

Suppose we have the following data, separated by semicolon (;), in a text file that we want to import into a MySQL file "A.txt" -

100;Ram;IND;15000
120;Mohan;IND;18000
Copy after login

Now with the help of the following query, we can import the data into the MySQL table by using the option "FIELDS SEPARATED BY" -

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

mysql> Select * from employee12_tbl;
+------+----------------+----------+--------+
| Id   | Name           | Country  | Salary |
+------+----------------+----------+--------+
| 100  | Ram            | IND      |  15000 |
| 120  | Mohan          | IND      |  18000 |
+------+----------------+----------+--------+
2 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How can we import data from a text file into a MySQL table using MySQL LOAD DATA INFILE statement with 'FIELDS TERMINATED BY' option?. 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