【发布时间】:2017-04-19 17:36:26
【问题描述】:
我在运行查询时收到错误“错误代码:'字段列表'中的未知列't2.FSVisitsToDate',但我无法确定我的查询哪里出错了。谁能指出我做错了什么?
INSERT INTO CMCustomer
(CustomerNumber,
LastName,
FirstName,
Address,
City,
State,
ZIPCode,
PhoneNo,
DriverLicenseNo,
SocialSecNo,
TaxExempt,
ExternalRefNumber,
AuxField,
Comments,
FSLevelNo,
FSDateOpened,
FSLastVisit,
FSVisitsToDate,
FSVisitsThisPeriod,
FSPurchaseToDate,
FSPurchaseThisPeriod,
FSDiscountToDate,
FSDiscountThisPeriod,
FSPointsToDate,
FSPointsThisPeriod,
FSPromoPointsToDate,
FSPromoPointsThisPeriod,
LastUpdated,
Employee)
SELECT t1.CustomerNumber,
t1.LastName,
t1.FirstName,
t1.Address,
t1.City,
t1.State,
t1.ZIPCode,
t1.PhoneNo,
t1.DriverLicenseNo,
t1.SocialSecNo,
t1.TaxExempt,
t1.ExternalRefNumber,
t1.AuxField,
t1.Comments,
t1.FSLevelNo,
t1.FSDateOpened,
t1.FSLastVisit,
t1.FSVisitsToDate,
t1.FSVisitsThisPeriod,
t1.FSPurchaseToDate,
t1.FSPurchaseThisPeriod,
t1.FSDiscountToDate,
t1.FSDiscountThisPeriod,
t1.FSPointsToDate,
t1.FSPointsThisPeriod,
t1.FSPromoPointsToDate,
t1.FSPromoPointsThisPeriod,
t1.LastUpdated,
t1.Employee
FROM cm01process t1
LEFT JOIN CMCustomer t2 ON t2.CustomerNumber = t1.CustomerNumber
ON DUPLICATE KEY UPDATE
t2.FSVisitsToDate = t2.FSVisitsToDate + t1.FSVisitsToDate,
t2.FSVisitsThisPeriod = t2.FSVisitsThisPeriod + t1.FSVisitsThisPeriod,
t2.FSPurchaseToDate = t2.FSPurchaseToDate + t1.FSPurchaseToDate,
t2.FSPurchaseThisPeriod = t2.FSPurchaseThisPeriod + t1.FSPurchaseThisPeriod,
t2.FSDiscountToDate = t2.FSDiscountToDate + t1.FSDiscountToDate,
t2.FSDiscountThisPeriod = t2.FSDiscountThisPeriod + t1.FSDiscountThisPeriod,
t2.FSPointsToDate = t2.FSPointsToDate + t1.FSPointsToDate,
t2.FSPointsThisPeriod = t2.FSPointsThisPeriod + t1.FSPointsThisPeriod,
t2.FSPromoPointsToDate = t2.FSPromoPointsToDate + t1.FSPromoPointsToDate,
t2.FSPromoPointsThisPeriod = t2.FSPromoPointsThisPeriod + t1.FSPromoPointsThisPeriod;
我想要完成的是从我的一个商店中获取一个文件并将其导入我的数据库。如果是新客户,我需要添加行,如果是重复客户,我需要更新字段(向用户添加积分)。
【问题讨论】:
-
您需要向我们展示您的查询,以便我们有机会查看您的查询有什么问题。
-
查询已附加。它就在代码块中,插入到.. from.. left join.. on duplicate key update query。
-
我不是mysql这个功能的专家,但不应该是
ON DUPLICATE KEY UPDATE FSVisitsToDate = t2.FSVisitsToDate + t1.FSVisitsToDate, ...你为什么要在这里设置t2.FSVisitsToDate。 (同样不是专家,但这感觉不对)。 -
t2 和 t1 是这两个表的别名。我正在从 cm01process (t1) 获取选择查询并将其插入 CMCustomer (t2)。如果这些行是新的,则它们被插入到表中,如果它们在表中已有重复键,则查询仅使用 (t2) 中的相应行的 (t2) 加上 (t1) 的值更新 (t2) 中的指定字段 (t1) t1).
标签: mysql insert-into on-duplicate-key