Home  >  Article  >  Database  >  How to Import Specific Columns from a CSV File into MySQL?

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

Linda Hamilton
Linda HamiltonOriginal
2024-11-15 08:56:02189browse

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.

The above is the detailed content of How to Import Specific Columns from a CSV File into MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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