2
我想更新列leaf_category
和TRUE
,其中類別不是父類別。它可以作爲一個select語句:PostgreSQL:更新時忽略了左外部自聯接
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
?
不會Postgres的讓你更新別名「 C1' ? – MatBailie 2012-01-07 02:57:20
@Dems:您可以使用別名來更新表,但'c1'關係在'FROM'子句中。在PostgreSQL中,與MySQL不同,一次只能更新*一個*表。如果我沒有弄錯,這對應於SQL標準 - 而額外的'FROM'子句是標準的PostgreSQL擴展。 – 2012-01-07 03:54:33
謝謝歐文。它的工作,它更短。通過在子條款中通過交換parent_id和id來實驗我們發現它返回根類別。 – slooow 2012-01-09 02:16:40