Deep unknown hierarchies often appear in real life, such as the company's employee organization relationship, regional relationship, tree structure in the file system, etc, you can clearly see the relationship between top and bottom during the presentation. How is the background implemented. The following describes two methods. First, sort by encoding from small to large (easy to display)
Deep unknown hierarchies often appear in real life, such as the company's employee organization relationship, regional relationship, tree structure in the file system, etc, you can clearly see the relationship between top and bottom during the presentation. How is the background implemented. The following describes two methods. First, sort by encoding from small to large (easy to display)
Deep unknown hierarchies often appear in real life, such as the company's employee organization relationship, regional relationship, tree structure in the file system, etc, you can clearly see the relationship between top and bottom during the presentation. How is the background implemented. The following describes two methods.
First: sort by encoding from small to large (easy to display)
It is implemented in two columns. The first column indicates encoding, and the second column indicates series, for example
00 1
0001 2
0002 2
000101 3
...
In the presentation, you only need to sort by encoding from small to large. However, the question here is how to determine whether the current node is a leaf node, the method used here is to determine whether the current value has a substring with the next value. If yes, It is not; otherwise it is a leaf node.
Type 2: Self-referenced parent-child relationship (poor implementation of SQL)
It is also implemented using two columns. The first column indicates the id of the auto-increment primary key, and the second column references the first column parentid, such
1 1
2 1
3 1
4 2
...
You need to know the maximum depth during the presentation. If there are n levels, the inner joins n-1 times.
From the lowest level to the highest level inner join: You can retain parentid each time.
Or you can join from the highest level to the lowest level: retain the id each time.
For example:
-- Select E. id, level1, level2, level3, E. name as level4 from (select D. id, level1, level2, D. name as level3 from (select B. id,. name level1, B. name level2 from region as A inner join region as Bon. id = B. parentid) as C inner join region as Don C. id = D. parentid) as D inner join region as Eon D. id = E. parentid -- select F. parentid, F. name level1, level2, level3, level4 from (select D. parentid, D. name level2, level3, level4 from (select. parentid,. name level3, B. name level4 from region as A inner join region as Bon. id = B. parentid) as C inner join region as Don C. parentid = D. id) as E inner join region as Fon E. parentid = F. id