首頁 > 資料庫 > mysql教程 > 如何將 CSV 檔案中的特定欄位匯入 MySQL?

如何將 CSV 檔案中的特定欄位匯入 MySQL?

Linda Hamilton
發布: 2024-11-15 08:56:02
原創
263 人瀏覽過

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
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板