ON重复密钥更新不起作用

时间:2016-06-13 19:56:42

标签: php mysql

我使用以下代码插入/更新MySQL表,但是当我使用ON Duplicate Key update时存在重复记录时它没有做任何事情。该代码非常适合插入,但我想更新如果描述与源不同,所以我添加了这个ON DUPLICATE KEY UPDATE PURCHASE_DESCRIPTION = VALUES ('$pdesc')",但它没有插入也没有更新

mysqli_query($con,
    "INSERT INTO  table_name
    (STOCK_NO, PURCHASE_DESCRIPTION, SALES_DESCRIPTION, itemId, itype, ITEM_DESCRIPTION, uOfM, uConvFact, poUOfM, lead, suplId, suplProdCode, minLvl, maxLvl, ordLvl, ordQty, unitWgt, sales, bomRev,  makebuy) VALUES
    ('{$itemid}', '{$pdesc}', '{$sdesc}', '{$itemId}', '{$itype}', '{$itemdsc}', '{$uOfM}', '{$uConvFact}', '{$poUOfM}', '{$lead}', '{$supplId}', '{$suplProdCode}', '{$minLvl}', '{$maxLvl}', '{$ordLvl}', '{$ordQty}', '{$unitWgt}', '{$sales}', '{$bomRev}', '{$makebuy}')
    ON DUPLICATE KEY UPDATE PURCHASE_DESCRIPTION = VALUES ('$pdesc')"
);

// STOCK_NO是主键

1 个答案:

答案 0 :(得分:0)

您的SQL不正确。看看:

mysqli_query($con,
"INSERT INTO  table_name
(STOCK_NO, PURCHASE_DESCRIPTION, SALES_DESCRIPTION, itemId, itype, ITEM_DESCRIPTION, uOfM, uConvFact, poUOfM, lead, suplId, suplProdCode, minLvl, maxLvl, ordLvl, ordQty, unitWgt, sales, bomRev,  makebuy) VALUES
('{$itemid}', '{$pdesc}', '{$sdesc}', '{$itemId}', '{$itype}', '{$itemdsc}', '{$uOfM}', '{$uConvFact}', '{$poUOfM}', '{$lead}', '{$supplId}', '{$suplProdCode}', '{$minLvl}', '{$maxLvl}', '{$ordLvl}', '{$ordQty}', '{$unitWgt}', '{$sales}', '{$bomRev}', '{$makebuy}')
ON DUPLICATE KEY 

-- HERE --
UPDATE PURCHASE_DESCRIPTION = '{$pdesc}' 
");

另一个选项是UPDATE PURCHASE_DESCRIPTION = VALUES(PURCHASE_DESCRIPTION)

您可以在此处阅读更多内容:http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html