我想用leaf_category
更新列TRUE
,其中类别不是父类别。它作为一个选择语句:
select
c1.id, c1.name, c1.slug, c1.level, c2.parent_id, c2.name, c2.slug, c2.level
from
catalog_category c1
left outer join
catalog_category c2 on
(c1.id = c2.parent_id)
where
c2.parent_id is null;
但是,相应的UPDATE
会将所有列设置为TRUE
。
update catalog_category
set leaf_category = True
from
catalog_category c1
left outer join
catalog_category c2 on
(c1.id = c2.parent_id)
where
c2.parent_id is null;
这样的UPDATE
是否可能?
答案 0 :(得分:9)
您只是缺少连接的WHERE子句:
UPDATE catalog_category
SET leaf_category = TRUE
FROM catalog_category c1
LEFT join catalog_category c2 ON c1.id = c2.parent_id
WHERE catalog_category.id = c1.id
AND c2.parent_id IS NULL;
这个带有NOT EXISTS的表格可能更快,同时也是这样:
UPDATE catalog_category
SET leaf_category = TRUE
WHERE NOT EXISTS (
SELECT *
FROM catalog_category c
WHERE c.parent_id = catalog_category.id
);