在MySQL中加载选定的CSV列

时间:2018-02-10 00:49:58

标签: mysql csv mariadb

我正在尝试使用以下方法将选定的列从CSV导入数据库:

LOAD DATA LOCAL INFILE 'local/path/to/file.csv' 
INTO TABLE product 
(ItemName,ItemShortDescription,RetailPrice,Brand,Category,@PRICE_1,@PRICE_2,@PRICE_3,Weight,Length,Width,Height) 
FIELDS TERMINATED BY '|' 
ESCAPED BY '"'
LINES TERMINATED BY '\n'  

..所以跳过CSV中的3列(PRICE_1,PRICE_2,PRICE_3)

product表:

CREATE TABLE `product` (
  `pid` int(255) NOT NULL AUTO_INCREMENT,
  `ItemName` varchar(255) DEFAULT NULL,
  `ItemShortDescription` varchar(255) DEFAULT NULL,
  `RetailPrice` varchar(255) DEFAULT NULL,
  `Brand` varchar(255) DEFAULT NULL,
  `Category` varchar(255) DEFAULT NULL,
  `Weight` varchar(255) DEFAULT NULL,
  `Length` varchar(255) DEFAULT NULL,
  `Width` varchar(255) DEFAULT NULL,
  `Height` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=11387 DEFAULT CHARSET=utf8mb4;

..但会导致错误。

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:1)

正如load data上的mysql文档所述,字段列表>字段/行分隔符后

LOAD DATA LOCAL INFILE 'local/path/to/file.csv' 
INTO TABLE product  
FIELDS TERMINATED BY '|' 
ESCAPED BY '"'
LINES TERMINATED BY '\n'
(ItemName,ItemShortDescription,RetailPrice,Brand,Category,@PRICE_1,@PRICE_2,@PRICE_3,Weight,Length,Width,Height)