0

我想通过将列乘以列来更新totaltable:中的列。tbl_ordersquantitycost

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' 进行更新

谁能看到我做错了什么?

4

1 回答 1

1

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

如果要更新 all orderid,请删除 allWHERE子句。

于 2013-10-09T02:08:45.643 回答