MYSQL分层数据使用RECURSIVE查找免费节点

时间:2019-07-12 07:55:48

标签: mysql mariadb

我能够获得所有节点的级别,但是我希望如果任何节点未满其节点,则该节点具有空白空间,然后查询返回其ID

每个级别都有固定数量的节点 例如。

  1. 级别NodeNumber

     1          2
     2          4
     3          8

等等。

逻辑是

  

POW(2,level)=每个级别的节点数
  例如POW(2,4)= 16

级别查询

with RECURSIVE category_path (id, name, lvl) AS
                   (
                       SELECT id,  name, 1 lvl
                       FROM users
                       WHERE parent =1
                       UNION ALL
                       SELECT   c.id, c.name,cp.lvl + 1 from category_path as cp JOIN users as c on cp.id = c.parent
                   )



SELECT * FROM category_path;

查询结果

 ID   NAME              LEVEL  PARENT



2   Dr. Dylan Lueilwitz    1    1
3   Dwight Carter MD       1    1
4   Prof. Daryl Sauer      2    2
5   Mohammad Kessler       2    2
6   Mr. Obie Bergnaum DDS  2    3
7   Prof. Stanley Maggio   2    3
8   Caroline Mertz         3    4
9   Helga Heidenreich PhD  3    4
10  Mr. Casimir Jacobs Sr. 3    5
11  Vernie Orn Jr.         3    5
12  Kasandra Kulas         3    6
13  Prof. Stuart Lemke     3    6
14  Dr. Elva Little DVM    3    7
15  Prof. Jamal McDermott  3    7
16  Lucious Dare           4    8
17  Gwendolyn Huels PhD    4    8
18  Mrs. Willow Aufderhar  4    9
19  Aimee Dicki MD         4    9
20  Lowell Huels I         4    10
In This result Level 4 have  5 Node But required 16 Node and Parent 10 have only 1 node 

So i want parent Id 10

我尝试了很多查询,但结果没有被接受

救救我

预先感谢

0 个答案:

没有答案