【发布时间】:2020-02-15 04:00:53
【问题描述】:
我有这个 SQL:
SELECT count(*) as ct
FROM classifications cls
WHERE
cls.classification_id = :classification_id
START WITH cls.classification_id = :root_classification_id
CONNECT BY NOCYCLE PRIOR cls.classification_id = cls.parent_id
并且需要将其迁移到 postgresql 10。
我已经安装了扩展 tablefunc 并尝试使用 connectedby。这是我的尝试:
SELECT count(*) as ct
FROM classifications cls
WHERE
cls.classification_id = :classification_id
union
SELECT count(classification_id) FROM connectby('classifications','classification_id','parent_id',:root_classification_id,5)
as t(classification_id varchar, parent_id varchar,level int)
问题是联合是错误的方式,因为这样你会得到 2 个计数结果。
【问题讨论】:
标签: sql postgresql database-migration recursive-query