gpt4 book ai didi

php - 准备 MySQL INSERT INTO ON DUPLICATE KEY UPDATE 的正确语法?

转载 作者:行者123 更新时间:2023-11-29 02:25:02 27 4
gpt4 key购买 nike

我一直在对我的代码进行故障排除,搜索 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);
...

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

我启用了尝试阅读反馈,但这太笼统了,没有可操作的见解。

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

希望您有一些见解可以分享?

谢谢!

最佳答案

为什么不继续使用 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);

关于php - 准备 MySQL INSERT INTO ON DUPLICATE KEY UPDATE 的正确语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973548/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com