How to define a level with unknown depth

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.