How to Customize Column Mappings During CSV Import into MySQL?

Susan Sarandon
Release: 2024-10-24 04:13:30
Original
430 people have browsed it

How to Customize Column Mappings During CSV Import into MySQL?

Importing CSV Files into MySQL with Custom Column Mappings

When importing CSV files into MySQL, there may be instances when the column names in the CSV file differ from those in the destination table. To address this, you can specify the mapping between CSV columns and database columns programmatically using the LOAD DATA INFILE statement.

The LOAD DATA INFILE syntax allows you to specify a column list, which defines the order in which the CSV columns should be imported into the database columns. For example:

LOAD DATA INFILE 'uniq.csv' INTO TABLE tblUniq
(uniqName, uniqCity, uniqComments)
Copy after login

In this example, the first column in the CSV file will be imported into the uniqName column in the database table, the second column into uniqCity, and so on.

If you only need to import a subset of the database table's columns, you can specify the column list accordingly:

LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);
Copy after login

For complex mappings or previewing purposes, consider using graphical clients like HeidiSQL, which allow you to visually configure the column order and generate the corresponding SQL query.

The above is the detailed content of How to Customize Column Mappings During CSV Import into MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!