id、parent_forum_post_idのテーブルフォーラム投稿があり、特定のid = 1221に対して、子がカウントされていることがわかりました。
with recursive all_posts (id, parentid, root_id) as (
select t1.id,
t1.parent_forum_post_id as parentid,
t1.id as root_id
from forumposts t1
union all
select c1.id,
c1.parent_forum_post_id as parentid,
p.root_id
from forumposts c1
join all_posts p on p.id = c1.parent_forum_post_id
)
select (count(*)-1) as child_count
from all_posts
where root_id=1221
group by root_id;
私が今必要としているのは、正反対です。特定のIDについて、そのレベルを確認します。これは、親の数によって決まります(親であり、親のparent_forum_post_id列でnullが見つかるまでは親の親です)。これが理にかなっていることを願っています。
どんな助けでも大歓迎です。ありがとう。