【发布时间】:2019-07-26 09:39:04
【问题描述】:
我正在开发一种用于更新网站中产品价格的工具。我目前遇到一个查询,该查询必须更新与另一个表中的主键链接的产品的特价:
TABLE product_special
product_id is the FK here
product_id | customer_group_id | price
--------------------------------------
15468 | 1 | 5,49
15468 | 2 | 3,60
15468 | 6 | 2,34
----------------------------------------
TABLE product
product_id is PK here
product_id | sku | price
--------------------------------------
15468 | 255 | 5,49
15468 | 500 | 3,60
15468 | 5377 | 2,34
我从 Excel 表格中获取 sku 和价格,这是我目前想出的,但它没有更新 product_special 中的价格:
foreach ($priceList as $sku => $price) {
$stmt = $dbh->prepare("UPDATE product_special
INNER JOIN product ON product.product_id = product_special.product_id
SET price = :price
WHERE sku = :sku AND customer_group_id = 1;");
$stmt->bindParam(':price', $price);
$stmt->bindParam(':sku', $sku);
if ($stmt->execute()){
$message = "<span style=\"color: #c4000b;\">Successfully updated client prices.</span>";
};
}
【问题讨论】:
-
我认为这是因为您的字段“price”和“sku”不明确,因为它们在两个表中都存在。将其更改为
SET product_special.price = :price WHERE product_special.sku = :sku您也可以考虑使用别名,这样您就不必每次都输入“product_Special” -
你为什么加入这些表?您不能只更新 customer_group_id = 1 的 product_special 的价格吗?
-
@TommyBs 是的,你是对的。我刚刚将“SET price = :price”更改为“SET product_special.price = :price”,它正在工作。谢谢。
-
我已将此更新为答案,因此您可以接受,以便其他人在遇到类似问题时可以看到此问题已解决
-
@forpas 没有 JOIN 就不行。