While importing data from a CSV file into MySQL using LOAD DATA INFILE, you may encounter issues aligning columns with the corresponding columns in the destination table. This is especially true when the columns in the CSV file are not in the same order as in the MySQL table.
To automatically assign columns during import, you can use the following syntax:
LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (col1, col2, col3, col4, col5...);
Here's a breakdown of the options:
For MySQL 8.0 users, using the LOCAL keyword is not recommended due to security concerns. You may receive an error. To address this, you can change the configuration by setting the secure-file-priv option in the MySQL configuration file to the directory containing the CSV file.
By using this syntax, you can ensure that each column in the CSV file is automatically assigned to the corresponding column in the MySQL table, regardless of their order in the CSV file.
The above is the detailed content of How Can I Automatically Assign Columns When Importing CSV Data into MySQL?. For more information, please follow other related articles on the PHP Chinese website!