【问题标题】:insert into... on duplicate update causes Unknown column in field list, but the field exists重复更新时插入...导致字段列表中的未知列,但该字段存在
【发布时间】: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


【解决方案1】:

我不知道为什么这会奏效,所以我希望有人解释一下,以便我有更好的理解,但如果我像这样构造语句的“重复...”部分,显然查询会成功完成:

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() 有人可以确认吗?

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2019-06-30
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多