Convert UPDATE with INNER JOIN from SQL for use in MySQL

前端 未结 4 2189
醉酒成梦
醉酒成梦 2021-01-07 03:40

I\'d like to convert this for us in MySQL:

UPDATE product 
SET price = 12.95 
FROM product 
    INNER JOIN product_to_category ON product.product_id = produc         


        
4条回答
  •  盖世英雄少女心
    2021-01-07 04:31

    I don't have your database, so I can't really test, but I suppose you could use the multi-table syntax for your update statement :

    UPDATE [LOW_PRIORITY] [IGNORE] table_references
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition]
    

    Quoting the manual :

    For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions.

    In your case, something like this might do the trick :

    UPDATE product, product_to_category, category
    SET product.price = 12.95 
    WHERE product.product_id = product_to_category.product_id 
        AND product_to_category.category_id = category.category_id 
        AND category.parent_id = 39
    

    Hope this helps !

提交回复
热议问题