MySQL在同一查询中选择并更新

时间:2015-02-20 09:00:37

标签: mysql sql join

是否可以选择并加入表,然后更新该表?

我基本上希望将特定供应商拥有的所有产品的价格提高5%。问题是定价细节保存在产品本身的单独表格中。

我知道这种语法不正确,但它会为您提供我想要实现的目标:

update 
    products_quantity_pricing set price = price + (price/100*5)
where (select 
    products.supplier_id,
    products_quantity_pricing.price 
    from products 
join
    products_quantity_pricing on products_quantity_pricing.product_id = products.id 
where products.supplier_id = 7 )

2 个答案:

答案 0 :(得分:2)

您将JOIN直接放入UPDATE查询:

UPDATE products_quantity_pricing AS pqp
JOIN products AS p ON pqp.product_id = p.id
SET price = price + (price/100*5)
WHERE p.supplier_id = 7

答案 1 :(得分:0)

update  products_quantity_pricing set price = price + (price/100*5)
where  products_quantity_pricing.price  in 
(select  products_quantity_pricing.price 
    from products  join
    products_quantity_pricing on products_quantity_pricing.product_id = products.id 
where products.supplier_id = 7 )