如果值包含逗号

时间:2015-07-23 23:15:43

标签: mysql csv import

我是一个CSV文件,其值可能是逗号作为值的一部分

"09200, France, Paris", "Tower ""Olivia"""
"09200, Spain, Barselona", Shop - perfect

但是,一旦我导入数据,它就会破坏4列的值(基于行中的逗号数)。我做错了什么?请参阅下面的导入命令。

LOAD DATA LOCAL INFILE '~/Downloads/file.csv' INTO TABLE my_table CHARACTER SET utf8 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@col1,@col2) set address=@col1,name=@col2;

1 个答案:

答案 0 :(得分:2)

在查询中添加ENCLOSED BY子句:

LOAD DATA LOCAL INFILE '~/Downloads/file.csv' 
  INTO TABLE my_table 
  CHARACTER SET utf8 
  FIELDS TERMINATED BY ',' 
  OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  (@col1,@col2) set address=@col1,name=@col2;