您可以通过两种方式使用LOAD DATA INFILE
.
首先按原样读取第一个字段值,然后在子句=
中从中删除等号和双引号。SET
此外,您很可能希望在加载数据时进行其他转换,例如:
NULL
当您的字段为空时设置实际s
- 从货币价值中去除美元符号
- 您可能必须转换日期值(但您的示例数据没有它们,因此没有信息可以推断)
LOAD DATA LOCAL INFILE '/path/to/your/file.csv'
IGNORE INTO TABLE table_name
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(@MID, DBAName, PartnerID, PartnerName, SubPartnerID, SubPartnerName, ActiveMonths,
@BonusAmount, @BonusAppliedDate, @PartnerPercentage, @PartnerShare, @TotalPayment)
SET MID = TRIM(BOTH '"' FROM SUBSTR(@MID, 2)), -- here we get rid of equal sign and double quotes
BonusAmount = TRIM(LEADING '$' FROM NULLIF(@BonusAmount, '')),
BonusAppliedDate = NULLIF(@BonusAppliedDate, ''),
PartnerPercentage = NULLIF(@PartnerPercentage, ''),
PartnerShare = TRIM(LEADING '$' FROM NULLIF(@PartnerShare, '')),
TotalPayment = TRIM(LEADING '$' FROM NULLIF(@TotalPayment, ''))
第二种方法是利用LINES STARTING BY
条款
LOAD DATA LOCAL INFILE '/path/to/your/file.csv'
IGNORE INTO TABLE table_name
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' STARTING BY '='
IGNORE 1 LINES
(MID, DBAName, PartnerID, PartnerName, SubPartnerID, SubPartnerName, ActiveMonths, @BonusAmount, BonusAppliedDate, PartnerPercentage, PartnerShare, @TotalPayment)
SET BonusAmount = TRIM(LEADING '$' FROM NULLIF(@BonusAmount, '')),
BonusAppliedDate = NULLIF(@BonusAppliedDate, ''),
PartnerPercentage = NULLIF(@PartnerPercentage, ''),
PartnerShare = TRIM(LEADING '$' FROM NULLIF(@PartnerShare, '')),
TotalPayment = TRIM(LEADING '$' FROM NULLIF(@TotalPayment, ''))
现在,如果您的目标表架构看起来像
CREATE TABLE table_name
(
MID BIGINT,
DBAName VARCHAR(100),
PartnerID INT,
PartnerName VARCHAR(100),
SubPartnerID INT,
SubPartnerName VARCHAR(100),
ActiveMonths INT,
BonusAmount DECIMAL(19, 2),
BonusAppliedDate DATE,
PartnerPercentage DECIMAL(3, 2),
PartnerShare DECIMAL(19, 2),
TotalPayment DECIMAL(19, 2)
);
然后在使用任一方法加载后,我们在表中得到
mysql> 从表名中选择 *;
+---------------+----------+------------+--------- ---+--------------+----------------+-------------- +-------------+------------------+---------------- ---+--------------+--------------+
| 中 | DBA名称 | 合作伙伴 ID | 合作伙伴名称 | 子合作伙伴ID | 子合作伙伴名称 | 活动月 | 奖金金额 | 奖金申请日期 | 合作伙伴百分比 | 合作伙伴分享 | 总付款 |
+---------------+----------+------------+--------- ---+--------------+----------------+-------------- +-------------+------------------+---------------- ---+--------------+--------------+
| 8788014065741 | 公司2 | 7968 | 我 | 11839 | 乔吹| 0 | 50.00 | 空 | 空 | 空 | 350.64 |
| 8788014065756 | 公司2 | 7968 | 你 | 11839 | 乔吹| 0 | 50.00 | 空 | 空 | 空 | 294.60 |
+---------------+----------+------------+--------- ---+--------------+----------------+-------------- +-------------+------------------+---------------- ---+--------------+--------------+
2 行(0.00 秒)