Problem:
You have a CSV file with numerous columns and need to selectively import only specific ones into a MySQL database using the LOAD DATA INFILE command.
Solution:
To load only selected columns into MySQL, follow these steps:
Example:
Run this command to import selected columns from a CSV file named file.csv into a table named t1:
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE t1 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@col1,@col2,@col3,@col4) SET name=@col4,id=@col2 ;
In this example, @col1, @col2, @col3, and @col4 are variables representing the selected CSV file columns, while name and id are the corresponding column names in the t1 table.
The above is the detailed content of How Can I Import Specific Columns from a CSV to MySQL Using LOAD DATA INFILE?. For more information, please follow other related articles on the PHP Chinese website!