混合UPDATE SELECT查询

时间:2015-01-08 23:15:10

标签: mysql sql

我需要运行UPDATE查询:

UPDATE products
    SET fcategory = SELECT fcategory 
                    FROM categories 
                    WHERE categories.scategory = products.scategory

即。对于"产品中的行"其中列scategory = categories.scategory,products.fcategory必须更新为categories.fcategory

示例:

categories.scategory monkies, categories.category gorilla
products.scategory monkies

=> products.fcategory必须更新为gorilla,因为products.scategory = monkies = products.scategory

任何人都知道如何编写这样的UPDATE查询?

感谢。

1 个答案:

答案 0 :(得分:2)

使用JOIN

UPDATE products AS p
JOIN categories AS c ON p.scategory = c.scategory
SET p.fcategory = c.scategory