Importing CSV Data into MySQL with Column Headers
Importing data from a CSV file into a MySQL table is a common task. While there are tools that can execute this operation with a GUI, many users prefer a command-line approach. In this article, we will explore a method that utilizes SQL syntax to import a CSV file, including the first row as column names.
Importing CSV Data
To import a CSV file into MySQL, use the following modified LOAD DATA syntax:
LOAD DATA LOCAL INFILE 'uniq.csv' INTO TABLE tblUniq FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (uniqName, uniqCity, uniqComments)
Explanation:
Alternative Approach
If this method does not work for you, consider writing a script to extract the column names from the CSV file and create a SQL statement that dynamically imports the data with the correct column headers.
The above is the detailed content of How to Import a CSV File with Headers into MySQL Using SQL?. For more information, please follow other related articles on the PHP Chinese website!