Home > Database > Mysql Tutorial > How to Handle Mismatched Column Names When Importing CSV Files into MySQL?

How to Handle Mismatched Column Names When Importing CSV Files into MySQL?

DDD
Release: 2024-12-15 16:41:13
Original
971 people have browsed it

How to Handle Mismatched Column Names When Importing CSV Files into MySQL?

Import CSV file directly into MySQL

At one point in your application, you might need to import a CSV file into MySQL. The import process can be achieved in various ways, and one of them is using the LOAD DATA INFILE MySQL command.

Using the above example, there is a possible scenario where column names in the CSV file and database table are different. How do you account for such a situation?

The solution lies in specifying which CSV column gets imported into which database column. The LOAD DATA INFILE syntax allows you to do that.

By default, when no column list is provided at the end of the LOAD DATA INFILE statement, input lines are expected to contain a field for each table column. If you want to load only some of a table's columns, specify a column list like this:

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

A good approach to use when you find the INFILE syntax too complicated is by using a graphical client like HeidiSQL to click together the proper column order (it has a graphical preview) and then copy and paste the generated SQL query.

The above is the detailed content of How to Handle Mismatched Column Names When Importing CSV Files into MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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