来自两个INNER JOINS的更新

时间:2014-03-04 16:33:55

标签: mysql sql sql-update inner-join

我想从几个INNER JOINS更新表INVENTAR中的列。 我有以下几点:
INVENTARPRODUCT_IDCATEGORY 表格PRODUCT_TO_CATEGORY包含PRODUCT_IDCATEGORY_ID列 表CATEGORY_DESCRIPTIONCATEGORY_IDNAME

我希望NAME列更新CATEGORY列。 这是我的代码:

UPDATE inventar
   SET inventar.category=category_description.name
  FROM inventar
       INNER JOIN product_to_category 
             ON product_to_category.product_id=inventar.product_id
       INNER JOIN category_description 
             ON category_description.category_id=product_to_category.category_id

1 个答案:

答案 0 :(得分:3)

正确的MySQL语法是:

UPDATE inventar i INNER JOIN
       product_to_category ptc
       ON ptc.product_id = i.product_id INNER JOIN
       category_description cd
       ON cd.category_id = ptc.category_id
   SET i.category = cd.name;

您的语法看起来更适合SQL Server或Postgres。