Is there a simple way to query the children of a node?

前端 未结 6 1038
余生分开走
余生分开走 2020-12-10 05:19

I\'ve been using the crap out of the Nested Set Model lately. I have enjoyed designing queries for just about every useful operation and view. One thing I\'m stuck on is how

6条回答
  •  猫巷女王i
    2020-12-10 06:03

    i know im doing a necro post, but here's my opinion.

    why not include a "depth" column in your nested set? the depth column will indicate the "level" of an item.

    so, to select the immediate childs of an item, just do

    select c.*
    from tree as p
    join tree as c on (c.left > p.left and c.right < p.right and c.depth = p.dept + 1) where p.id = @parentID

提交回复
热议问题