MySQL determines which are leaf nodes, molecular nodes, and root nodes

Note: The test database version is MySQL 8.0

If you need scott users to create tables and enter data statements, please refer to:
scott create tables and enter data sql scripts

1. Demand

Determine which types of nodes a given row belongs to: leaf nodes, branch nodes, and root nodes.

For this example, the leaf node indicates that the employee is not a manager; the employee on the branch node is the manager, and the root node with the manager is the employee without a manager.
By returning 1 (TRUE) or 0 (FALSE),

2. Solution

Please note that the EMP table is a tree hierarchical model, not a recursive hierarchical model, and the mgr value of the root node is null.
If emp is a recursive hierarchical model, the root node should have self-reference (that is, the mgr value of employee KING will be empno of KING).
In fact, self-referencing is not intuitive, so the mgr of the root node is set to a null value here.
For the use of connect by (Oracle) or with in the recursive hierarchical model, please be careful: SQL may have an infinite loop. If you must use the recursive hierarchy, you must consider avoiding such loops in the code.

select  e.ename,
        (select sign(count(*)) from emp d
          where 0 =
            (select count(*) from emp f
              where f.mgr = e.empno)) as is_leaf,
        (select sign(count(*)) from emp d
          where d.mgr = e.empno
            and e.mgr is not null) as is_branch,
        (select sign(count(*)) from emp d
          where d.empno = e.empno
            and d.mgr is null) as is_root
  from  emp e
 order  by 4 desc,3 desc;

Test Record:

mysql> select  e.ename,
    ->         (select sign(count(*)) from emp d
    ->           where 0 =
    ->             (select count(*) from emp f
    ->               where f.mgr = e.empno)) as is_leaf,
    ->         (select sign(count(*)) from emp d
    ->           where d.mgr = e.empno
    ->             and e.mgr is not null) as is_branch,
    ->         (select sign(count(*)) from emp d
    ->           where d.empno = e.empno
    ->             and d.mgr is null) as is_root
    ->   from  emp e
    ->  order  by 4 desc,3 desc;
+--------+---------+-----------+---------+
| ename  | is_leaf | is_branch | is_root |
+--------+---------+-----------+---------+
| KING   |       0 |         0 |       1 |
| JONES  |       0 |         1 |       0 |
| BLAKE  |       0 |         1 |       0 |
| CLARK  |       0 |         1 |       0 |
| SCOTT  |       0 |         1 |       0 |
| FORD   |       0 |         1 |       0 |
| SMITH  |       1 |         0 |       0 |
| ALLEN  |       1 |         0 |       0 |
| WARD   |       1 |         0 |       0 |
| MARTIN |       1 |         0 |       0 |
| TURNER |       1 |         0 |       0 |
| ADAMS  |       1 |         0 |       0 |
| JAMES  |       1 |         0 |       0 |
| MILLER |       1 |         0 |       0 |
+--------+---------+-----------+---------+
14 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/u010520724/article/details/114128175