How does the left and right value tree of mysql calculate the node data of a certain layer?

the company does direct sales system, because of business needs, to customers to do a two-track membership system design. Inside the membership relationship uses the left and right value calculation.

double-track means that each node can only have at most two direct child nodes.

The

left area is the list of nodes on the left under a node
right, and the list of nodes on the right under a node

to settle the collision bonus, I now need to calculate that with a layer as the root node, I can get the left or right node data of the N layer .
my table structure data is like this,

:kt_relation
relation_id :
user_id :id
Lft:
Rgt:
Lft_sub:id0
Rgt_sub:id0.
Level:

10001Nsql


:Lft+Rgt<=Lft+Rgt,<=3

now I want to query, such as the yellow user_id in the above picture is 10003. This node, which is the layer 3 in the left area, should be the green two nodes. It"s stuck here now. I can"t move it.

my own idea is that we should start from the root node with the above sql. The third layer of 10003 should be the fourth layer of 10001, and 10003 is in the right area of 10001. Then I will exclude all the left area of 10001, and only inquire how many nodes there are in the right area of layer 4 of 10001. 10013 is obviously included. This is blocked again, and it doesn"t feel like it will work. Even if it works, there is a problem. It takes many sql statements to complete the final result. Is there any way to use only one sentence like the one above to produce the result, and the query takes time in 0.035sjournal 0.02s.

because I don"t want to be too time-consuming here, there are a lot of bonus inquiries and updates that will take time. The query time should be as short as possible. I indexed it.

I don"t know if I made it clear. There are friends who have studied the left and right value algorithms, pointing and pointing.

Apr.11,2021

SELECT Child.user_id, Child.Lft, Child.Rgt,Child.Level
            FROM kt_relation as Child, kt_relation as Parent
                WHERE
                  Child.Level = 3+1
                  -- AND Child.Lft+Child.Rgt <= Parent.Lft +Parent.Rgt   -- 
                                    
                                    AND Child.Lft+Child.Rgt > Parent.Lft +Parent.Rgt    -- 
                  
                                    AND Parent.user_id = 10001
                                    
                                    AND Child.Lft+Child.Rgt <= 12 +25

has come up with a sql statement that can be implemented. The premise is to confirm which area of the topmost node 10001 is this node, then check the right area of 10001, and then add a condition, query the left area of 10003, 12-25 can represent 10003.
this kind of self-connection makes me a little dizzy! The speed is OK, maybe it's because of the index. In the
php, first get the record of the node 10003 to be checked, and take out his left and right values.
then calculates which layer should start with the root node. For example, if you want to check the third floor at the beginning of this 10003, you should actually check the fourth floor of 10001. Because there is a difference between 10003 and 10001. So it's the 3 + 1 floor. What a mess.

come up with a simple one, first find out all the descendant nodes of this node, add conditions, and the sum value is less than itself, and then define the layer. Just came out.
SELECT * FROM Tree WHERE Lft > 1 AND Lft < 26 AND Lft + Rgt < = 1 + 26 AND Level = 4

Why, I wrote down the problem, and then soon, I solved the problem by myself. It's strange. If you don't write it, you can't finish it. Actually, I'm just a dish b

.

Hello, can you give me the structure of the kt_relation table? I have been working on a project recently, which is also of this type.
in addition, what data will need to be modified when a record is inserted under the current tree? Thank you.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3f9ef-2bc24.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3f9ef-2bc24.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?