正确的语法准备MySQL INSERT INTO在DUPLICATE KEY UPDATE?

时间:2014-05-31 18:20:22

标签: php mysql sql

我一直在对我的代码进行故障排除,搜索stackoverflow以找到使用PHP执行此操作的正确语法。我无法弄清楚ON DUPLICATE KEY UPDATE语法如何与预准备语句一起使用:

INSERT INTO placements_by_date (DateVal, PlacementName, PlacementId, 
CampaignId, AdName, Format, TagId, Impressions, Clicks, Leads, MediaCost) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
ON DUPLICATE KEY UPDATE 
// HERE COMES THE TRICKY SYNTAX: 
DateVal = VALUES(DateVal), PlacementName = VALUES(PlacementName), 
PlacementId = VALUES(PlacementId), CampaignId = VALUES(CampaignId), 
AdName = VALUES(AdName), Format = VALUES(Format), TagId = VALUES(TagId), 
Impressions = ?, Clicks = ?, Leads = ?, MediaCost = ?

我尝试了很多变化:

...ON DUPLICATE KEY UPDATE 
DateVal = VALUES('$DateVal'), PlacementName = VALUES('$PlacementName'), 
PlacementId = VALUES('$PlacementId'), CampaignId = VALUES('$CampaignId'), 
AdName = VALUES('$AdName'), Format = VALUES('$Format'), TagId = VALUES('$TagId'),
Impressions = VALUES(?), Clicks = VALUES(?), Leads = VALUES(?), 
MediaCost = VALUES(?)";

我将其存储为字符串$sql,然后执行常规操作:

$mysqli = new mysqli(...
...
$stmt = $mysqli -> prepare($sql);
$stmt -> bind_param('sissiiiis', $PlacementName, $PlacementId, 
                     $AdName, $Format, $TagId, $Impressions, 
                     $Clicks, $Leads, $MediaCost);
...

但也许参数应该以不同的方式绑定?

我已尝试通过反馈阅读,但这太通用了,没有可操作的见解。

$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL;

希望您有一些分享的见解?

谢谢!

1 个答案:

答案 0 :(得分:2)

为什么不继续使用VALUES()方法?

INSERT INTO placements_by_date(DateVal, PlacementName, PlacementId, 
                               CampaignId, AdName, Format, TagId,
                               Impressions, Clicks, Leads, MediaCost
                              ) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    ON DUPLICATE KEY UPDATE 
        DateVal = VALUES(DateVal), PlacementName = VALUES(PlacementName), 
        PlacementId = VALUES(PlacementId), CampaignId = VALUES(CampaignId), 
        AdName = VALUES(AdName), Format = VALUES(Format), TagId = VALUES(TagId), 
        Impressions = VALUES(Impressions), Clicks = VALUES(Clicks), Leads = VALUES(Leads),
        MediaCost = VALUES(MediaCost);

您没有指定unique键是什么。但是,您不需要在update语句中包含这些列,因为它们已经相同。

如果您希望增加最后四个值而不是仅仅分配它们,那么您也可以这样做:

    ON DUPLICATE KEY UPDATE 
        DateVal = VALUES(DateVal), PlacementName = VALUES(PlacementName), 
        PlacementId = VALUES(PlacementId), CampaignId = VALUES(CampaignId), 
        AdName = VALUES(AdName), Format = VALUES(Format), TagId = VALUES(TagId), 
        Impressions = Impressions + VALUES(Impressions),
        Clicks = Clicks + VALUES(Clicks),
        Leads = Leads + VALUES(Leads),
        MediaCost = MediaCost + VALUES(MediaCost)

编辑:

以下内容仅适用于更新最后​​四列:

INSERT INTO placements_by_date(DateVal, PlacementName, PlacementId, 
                               CampaignId, AdName, Format, TagId,
                               Impressions, Clicks, Leads, MediaCost
                              ) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    ON DUPLICATE KEY UPDATE 
        Impressions = VALUES(Impressions), Clicks = VALUES(Clicks), Leads = VALUES(Leads),
        MediaCost = VALUES(MediaCost);