Read CSV file data into table

Use "load data local in file" to load the CSV file data into the table. Specify "," as the delimiter and "" "in the character string enclosure.

load data local infile'/tmp/book.csv'
  into table book fields terminated by','
  optionally enclosed by'"';

If the CSV column order and the field order are different, it cannot be read correctly.

To make each column in the CSV file correspond to a field:

load data local infile'/tmp/book.csv'
  into table book fields terminated by','
  optionally enclosed by'"'
(id, price, name, author_id);

If you want to skip the column, use an appropriate variable name.

load data local infile'/tmp/book.csv'
  into table book fields terminated by','
  optionally enclosed by'"'
(id, price, @tmp1, name, author_id);

Output the contents of the table as CSV

The method of outputting the contents of the table as CSV is explained below.

Associated Information