I want to update the column leaf_category
with TRUE
where the category is not a parent category. It works as a select statement:
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;
However, the corresponding UPDATE
sets all the columns to 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;
Is an UPDATE
like that possible at all?
You are just missing a connecting WHERE
clause:
UPDATE catalog_category c
SET leaf_category = true
FROM catalog_category c1
LEFT JOIN catalog_category c2 ON c1.id = c2.parent_id
WHERE c.id = c1.id
AND c2.parent_id IS NULL;
This form with NOT EXISTS
is probably faster, doing the same:
UPDATE catalog_category c
SET leaf_category = true
WHERE NOT EXISTS (
SELECT FROM catalog_category c1
WHERE c1.parent_id = c.id
);
The manual for UPDATE
.
Related:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With