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,...);
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!