将csv导入mysql时忽略列上的前两个字符

时间:2016-09-22 16:24:35

标签: mysql csv

我正在尝试将csv文件导入mysql表,但我需要在导入到mysql之前删除特定列上的前两个字符。 这是我的意见:

 string strLoadData = "LOAD DATA LOCAL  INFILE 'E:/park/Export.csv' INTO TABLE tickets  FIELDS  terminated by ',' ENCLOSED BY '\"'  lines terminated by '\n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,IssueReasonCode,IssueReason,NoticeLocation,Points,Notes)";

IssueReasoncode' has data like 'LU12' ,但是我需要删除前2个字符,它应该只有整数而不是字母数字。 我需要从该列中删除'LU'

是否可以在left(IssueReasonCode +' '2).上这样写这个列是varchar(45),由于上面有大量数据,现在无法更改。

由于

2 个答案:

答案 0 :(得分:1)

LOAD DATA INFILE能够在您(q.v. here)中读取每列的数据时执行该功能。在您的情况下,如果您想从IssueReasonCode列中删除前两个字符,则可以使用:

RIGHT(IssueReasonCode, CHAR_LENGTH(IssueReasonCode) - 2)

删除前两个字符。您可以使用LOAD DATASET语句末尾指定此类列映射。您的陈述应如下所示:

LOAD DATA LOCAL INFILE 'E:/park/Export.csv' INTO TABLE tickets
FIELDS terminated by ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(SiteId, DateTime, Serial, DeviceId, AgentAID, VehicleRegistration, CarPark, SpaceNumber,
GpsAddress, VehicleType, VehicleMake, VehicleModel, VehicleColour, IssueReasonCode,
IssueReason, NoticeLocation, Points, Notes)
SET IssueReasonCode = RIGHT(IssueReasonCode, CHAR_LENGTH(IssueReasonCode) - 2)

答案 1 :(得分:0)

引用this并引用此示例,您可以尝试以下操作来查看它是否有效

  

SET子句中的用户变量可以通过多种方式使用。该   以下示例直接使用第一个输入列作为值   t1.column1,并将第二个输入列分配给用户变量   在用于之前进行分割操作   t1.column2的值:

     

LOAD DATA INFILE'file.txt'INTO表t1(column1,@ var1)SET   column2 = @ var1 / 100;

string strLoadData = "LOAD DATA LOCAL  INFILE 'E:/park/Export.csv' INTO TABLE tickets  FIELDS  terminated by ',' ENCLOSED BY '\"'  lines terminated by '\n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,@IRC,IssueReason,NoticeLocation,Points,Notes) SET IssueReasonCode = substr(@IRC,2) ;";