Mysql使用更新与子查询

时间:2015-02-12 10:13:03

标签: mysql

我想通过在where子句中使用子查询来更新表,但是我得到了异常"您无法在FROM子句'&#34中指定目标表' catalog_category以进行更新;。这是我的问题:

update catalog_category set `status` = "inactive"
where id_catalog_category in ( 
SELECT id_catalog_category
         FROM catalog_category t1 where (SELECT status
         FROM catalog_category t2
         WHERE t2.lft < t1.lft AND t2.rgt > t1.rgt
         ORDER BY t2.rgt-t2.lft ASC limit 1) = 'active' and status = 'inherited_inactive')

有什么方法可以解决这个错误吗?

1 个答案:

答案 0 :(得分:1)

使用以下sql进行更新: -

update catalog_category cc
      join (SELECT id_catalog_category
                 FROM catalog_category t1 where (SELECT status
                 FROM catalog_category t2
                 WHERE t2.lft < t1.lft AND t2.rgt > t1.rgt
                 ORDER BY t2.rgt-t2.lft ASC limit 1) = 'active' and status = 'inherited_inactive') tmp
    on cc.id_catalog_category = tmp.id_catalog_category
    set cc.`status` = "inactive"