phpmysqlnested-set-model

Nested sets how to determine the left and right nodes


 1 - Root - 20

 2 - child1 - 3
 4 - child2 - 5
 6 - child3 - 7
 8 - child4 - 9

This is a nested set model of mysql.

let's say that the _lft and _rgt are the fields which indicates the left and right values

as I see the lower the value is the lefter are the child? so here we got 2 childs from the left side, and 2 childs from the right side, can be this done without adding a new field like direction which may indicate the child direction before inserting it?

I want to determine this only using _lft and _rgt, I can't figure out what is the logic, even in words, no code nessesary

How can I make a query only those childs who belongs to the left side relative to the parent?

any suggestions please, thanks.


Solution

  • I think you really need to work on understanding how the nested set works.

    I would read this for the diagram of the model and how new nodes are inserted (the rest is good info, too). http://falsinsoft.blogspot.com/2013/01/tree-in-sql-database-nested-set-model.html

    Note that all leaves are _rgt = _lft + 1. The _lft of the left most leaf is min(select _lft .... where _rgt = _lft + 1).

    Combine this knowledge with the path to a node query on that same page and I think you'll have what you need.

    An additional resource is the wikipedia page:

    http://en.wikipedia.org/wiki/Nested_set_model