mysql - Selecting a node in the nested set model through a not-unique name -
i trying manage retrieval of node in nested set model table, not through unique id, through name (a string), , other nodes within tree under different parents may called same way.
as far used unique id nodes inside nested sets:
select node.name, node.lft, node.rgt tbl parent, tbl node node.lft between parent.lft , parent.rgt , node.id = '{$node_id}' group node.id
trying extend method more general way retrieve node through name, came query containing having clauses depth of node retrieve, checking node name , depth:
select node.name, node.lft, node.rgt, count(node.id) depth tbl parent, tbl node node.lft between parent.lft , parent.rgt group node.id having (node.name = 'myparentname' , depth = 1) or (node.name = 'myparent2name' , depth = 2) or ... # , on
but not perfect: having 2 nodes same name , same depth, within different parents, both retrieved, no matter hierarchy belong to.
example:
articles | +--php | +--the-origins | +--syntax +--js +--history +--syntax
in case, query above return either articles/php/syntax or articles/js/syntax: "syntax" node depth 3, infact, either under php node or under js node. there effective path walk, solve problem?
i'm not quite sure you're trying here. trying access node pathname articles/php/syntax
? if you'd need self-join each parent level:
select n0.* tbl n0 join tbl n1 on n0.lft between n1.lft+1 , n1.rgt join tbl n2 on n1.lft between n2.lft+1 , n2.rgt n0.name='syntax' , n1.name='php' , n2.name='articles'
eta re comments: doing direct-child matches in nested set isn't fun. you'd have try select intermediate parent row between each joined row. row don't want exist, invert condition null left join. eg.:
select n0.* tbl n0 join tbl n1 on n0.lft between n1.lft+1 , n1.rgt join tbl n2 on n1.lft between n2.lft+1 , n2.rgt left join tbl nn1 on nn1.lft between n1.lft+1 , n0.lft-1 left join tbl nn2 on nn2.lft between n2.lft+1 , n1.lft-1 nn1.lft null , nn2.lft null , n0.name='syntax' , n1.name='php' , n2.name='articles'
Comments
Post a Comment