如何将 CSV 文件中的特定列导入 MySQL?

Linda Hamilton
发布: 2024-11-15 08:56:02
原创
185 人浏览过

How to Import Specific Columns from a CSV File into MySQL?

Selective Column Import from CSV to MySQL Database via LOAD DATA INFILE

When working with large comma-separated value (CSV) files, it becomes necessary to selectively import specific columns into a MySQL database. The LOAD DATA INFILE command offers a convenient way to achieve this goal.

To selectively import chosen columns from a CSV file, use the following syntax:

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE t1 
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'  
(@col1,@col2,@col3,@col4) set name=@col4,id=@col2 ;
登录后复制

Here's a breakdown:

  • @col1,2,3,4 represent variables used to store the CSV file's columns (presuming there are four columns).
  • name,id represent the table columns that will receive the imported data.

By following these steps, you can efficiently extract and import only the desired columns from a CSV file into your MySQL database.

以上是如何将 CSV 文件中的特定列导入 MySQL?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板