Ask Your Question
4

How can I selectively bring in particular sections or columns from a CSV file into mySQL database?

asked 2023-06-15 15:25:48 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-15 15:31:01 +0000

lakamha gravatar image

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 file
  • table_name with the name of the table you want to import the data into
  • column1, column2, column3, ... with the names of the columns you want to import

You 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-15 15:25:48 +0000

Seen: 11 times

Last updated: Jun 15 '23