2017-04-19 88 views
0

我收到错误“错误代码:未知列‘t2.FSVisitsToDate’在‘字段列表’当我跑我的查询,但我无法揣摩出我的查询是错误的。任何人都可以指出我做错了什么?INSERT INTO ...上的重复更新导致字段列表未知列,但该领域存在

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; 

我试图做到的是从我的商店之一采取文件,并将其导入到我的数据库。如果是一个新的客户,我需要添加的行,如果它是一个重复的客户,我需要更新的字段(加分的用户)。

+0

你需要证明你的查询我们必须在看到有什么不对您的查询的机会。 –

+0

该查询已附加。它正好在代码块中,插入..从..左连接..在重复密钥更新查询。 –

+0

我在MySQL的这个功能没有专家,但它不应该是'对重复密钥更新 FSVisitsToDate = t2.FSVisitsToDate + t1.FSVisitsToDate,...'你为什么想在这里设置't2.FSVisitsToDate' 。 (再次没有专家,但这种感觉不对)。 – JNevill

回答

0

我不知道为什么这个工作,所以我很想有人EXPL艾因所以我有一个更好的了解,但显然在查询成功完成,如果我结构语句,像这样的“关于重复......”部分:

ON DUPLICATE KEY UPDATE   
     CMCustomer.FSVisitsToDate = CMCustomer.FSVisitsToDate + values(FSVisitsToDate), 
     CMCustomer.FSVisitsThisPeriod = CMCustomer.FSVisitsThisPeriod + values(FSVisitsThisPeriod), 
     CMCustomer.FSPurchaseToDate = CMCustomer.FSPurchaseToDate + values(FSPurchaseToDate), 
     CMCustomer.FSPurchaseThisPeriod = CMCustomer.FSPurchaseThisPeriod + values(FSPurchaseThisPeriod), 
     CMCustomer.FSDiscountToDate = CMCustomer.FSDiscountToDate + values(FSDiscountToDate), 
     CMCustomer.FSDiscountThisPeriod = CMCustomer.FSDiscountThisPeriod + values(FSDiscountThisPeriod), 
     CMCustomer.FSPointsToDate = CMCustomer.FSPointsToDate + values(FSPointsToDate), 
     CMCustomer.FSPointsThisPeriod = CMCustomer.FSPointsThisPeriod + values(FSPointsThisPeriod), 
     CMCustomer.FSPromoPointsToDate = CMCustomer.FSPromoPointsToDate + values(FSPromoPointsToDate), 
     CMCustomer.FSPromoPointsThisPeriod = CMCustomer.FSPromoPointsThisPeriod + values(FSPromoPointsThisPeriod); 

我认为,你可以参考你插入到表并且您从中插入的表必须使用VALUES()任何人都可以确认吗?

相关问题