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

前端 未结 6 1028
余生分开走
余生分开走 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 06:13

    I found Wikipedia link has good minimized version of answer along with selected answer.

    SELECT DISTINCT Child.Name
    FROM ModelTable AS Child, ModelTable AS Parent 
    WHERE Parent.Lft < Child.Lft AND Parent.Rgt > Child.Rgt  -- associate Child Nodes with ancestors
    GROUP BY Child.Name
    HAVING MAX(Parent.Lft) = @parentId  -- Subset for those with the given Parent Node as the nearest ancestor
    

    And, any of you try to express it with Linq, please follow the link: https://stackoverflow.com/a/25594386/361100

提交回复
热议问题