Importing data from a CSV file into MySQL is often encountered, especially when dealing with large datasets. However, what happens when the CSV file's columns are not in the same order as the target MySQL table's columns?
To address this issue, the LOAD DATA INFILE command provides a solution. By specifying the column names explicitly, we can control which CSV column corresponds to each target table column. Consider the following syntax:
LOAD DATA INFILE 'abc.csv' INTO TABLE abc FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (col1, col2, col3, col4, col5...);
For MySQL 8.0 users, the LOCAL keyword is set to False by default, which introduces security risks. To overwrite this, follow the instructions in the MySQL documentation. However, this does not resolve the security issue but acknowledges that you are willing to take the risk.
The above is the detailed content of How Can I Import a CSV File into MySQL When Column Orders Don't Match?. For more information, please follow other related articles on the PHP Chinese website!