将拆分值从excel存储到mysql两行

时间:2014-12-16 15:34:38

标签: mysql

我有以下列的Excel表格

Sam/Simon   Date    Store   Customer name   Original Order Number

Simon    09/11/2014 Bristol Cr  Car        20089691/ 26089697

我需要将这些信息存储在表格中的两行

Simon    09/11/2014 Bristol Cr  Car        20089691/
Simon    09/11/2014 Bristol Cr  Car        26089697

我需要知道表结构。将逗号分隔的值从结果中的一列拆分为两行,并将它们从excel导出到mysql。

实际的表结构如下。

CREATE TABLE "tblOrderR" (
  "intOrderRemedialId" int(10) unsigned NOT NULL AUTO_INCREMENT,
  "intOrderId" int(10) unsigned NOT NULL,
  "intOrderRemedialGivenPence" int(10) unsigned NOT NULL,
  "intRequestedById" smallint(5) unsigned DEFAULT NULL,
  "intAuthorizedById" smallint(5) unsigned DEFAULT NULL,


  PRIMARY KEY ("intOrderRemedialId"),
  KEY "tblOrderRemedial" ("intOrderId"),
  KEY "tblOrderRemedial_ibfk_2" ("intRequestedById"),
  KEY "tblOrderRemedial_ibfk_3" ("intAuthorizedById"),
  CONSTRAINT "tblOrderRemedial_ibfk_1" FOREIGN KEY ("intOrderId") REFERENCES "tblOrder" ("intOrderId"),

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Order Remedial Information';

2 个答案:

答案 0 :(得分:0)

解决这个问题的一种方法是:

1)使用TEXT TO COLUMNS

将“原始订单号”Excel列拆分为两个单独的列

2)以csv格式保存数据

3)将数据加载到类似于此

的临时表中
samSimon,date,store,customerName,originalOrder1,originalOrder2

4)在最终表中运行两个插入:一个使用originalOrder1,另一个使用originalOrder2。例如:

insert into tblOrderR (columns) 
select samSimon,date,store,customerName,originalOrder1 from stagingTable;

insert into tblOrderR (columns) 
select samSimon,date,store,customerName,originalOrder2 from stagingTable;

非常伪代码答案,但希望你得到它的要点!

答案 1 :(得分:0)

只需导入两次。

第一次(示例代码,您可能需要调整一个或另一个):

LOAD DATA LOCAL INFILE '/path/to/file/excel.csv'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(column1, column2, @my_variable)
SET column3 = SUBSTRING(@my_variable FROM 1 FOR LOCATE('/', @my_variable));

第二次:

LOAD DATA LOCAL INFILE '/path/to/file/excel.csv'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(column1, column2, @my_variable)
SET column3 = SUBSTRING(@my_variable FROM LOCATE('/', @my_variable) + 1);

相关手册页: