MySQL 错误 1093 - 无法为 FROM 子句中的更新指定目标 table(两个连接)

MySQL Error 1093 - Can't specify target table for update in FROM clause (two joins)

我正在尝试使用来自 sales_orders 的 applied_discount 字段的计算来更新 sales_order_parts 详细信息 table 中的 unit_price。还有,价格是在master_part_listtable那样的价格。当我从 select 语句向下 运行 时,它 运行 很好并且 returns 来自 order_number 209 的值列表具有正确的计算。当我 运行 它从更新行完成时,它 returns "Error Code: 1093. You can't specify target table 'sop' for update in FROM clause" 有什么想法吗?

update sales_order_parts as sop
    set unit_price =
    (select (master_part_list.price * (1-(so.applied_discount/100)))
    from sales_orders as so
        inner join sales_order_parts as sop2 
            on so.id = sop2.order_id
        inner join master_part_list 
            on sop2.part_id = master_part_list.id
        where so.order_number = 209);

对 sales_order_parts 使用子查询。 mysql 然后视为新的临时 table

喜欢

update sales_order_parts as sop
    set unit_price =
    (select (master_part_list.price * (1-(so.applied_discount/100)))
    from sales_orders as so
        inner join (SELECT * FROM sales_order_parts) as sop2 
            on so.id = sop2.order_id
        inner join master_part_list 
            on sop2.part_id = master_part_list.id
        where so.order_number = 209);

您可以尝试不带子查询的连接

update sales_order_parts as sop
INNER JOIN sales_orders as so on so.id = sop.order_id
  AND so.order_number = 209 
inner join master_part_list on sop.part_id = master_part_list.id
SET sop.unit_price = master_part_list.price * (1-(so.applied_discount/100))