[英]MySQL - Multiple 2 Columns, Update within Same Table
我想通過將quantity
列乘以cost
列來更新表tbl_orders
的total
列。
UPDATE `tbl_orders` SET
total = (SELECT quantity * cost AS n1 FROM `tbl_orders` WHERE orderid = 167 LIMIT 1)
WHERE orderid= 167 LIMIT 1
我之前已經完成了子查詢更新,但是返回的mysql錯誤是:
您不能在FROM子句中指定目標表“ tbl_orders”進行更新
誰能看到我在做什么錯?
JOIN
吧。
UPDATE tbl_orders a
INNER JOIN
(
SELECT orderid, quantity * cost totalCost
FROM tbl_orders
WHERE orderid = 167
) b ON a.orderid = b.orderid
SET a.total = b.totalCost
WHERE a.orderid = 167
如果要更新所有orderid
,請刪除所有WHERE
子句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.