To selectively import particular sections or columns from a CSV file into a mySQL database, you can use the LOAD DATA command with the following syntax:
LOAD DATA [LOCAL] INFILE 'file.csv'
[REPLACE | IGNORE]
INTO TABLE table_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(column1, column2, column3, ...)
In this command, you need to replace:
file.csv
with the path to your CSV filetable_name
with the name of the table you want to import the data intocolumn1, column2, column3, ...
with the names of the columns you want to importYou can also add more options to the LOAD DATA command to customize the behavior, such as specifying a different delimiter or enclosure character, or setting a different line terminator.
Note that if you don't want to import all columns from the CSV file, you need to list only the columns you want to import in the last line of the command, and make sure the column names match the order and syntax in the CSV file. You can also skip the optional IGNORE 1 LINES
statement if your CSV file does not have a header row.
Asked: 2023-06-15 15:25:48 +0000
Seen: 11 times
Last updated: Jun 15 '23