获取父节点的所有子节点

时间:2020-08-06 04:43:58

标签: mysql treeview yii2-advanced-app kartik-v

我有一个treeview,它是根据Kartik Tree Manager呈现的。下面是我的树状视图

enter image description here

表格

enter image description here

我想做什么?

我想通过Floor-1查询选择MySQL的所有子节点

我试图运行如下查询

SELECT * FROM `mdc_node` m 
WHERE m.`lft` = 11-2

输出

enter image description here

所需的输出

我想要以下输出

------------------------------------------------------------
| `id` | `root` | `lft` | `rgt` | `lvl` | `name`   | 'icon' |
------------------------------------------------------------
| 3    |    1   |   3   |    4  |   2   |GIS Office| folder |
| 4    |    1   |   5   |    6  |   2   |   Ali    |  user  |
| 5    |    1   |   7   |    8  |   2   |   Usman  |  user  |
| 6    |    1   |   9   |    10 |   2   |  Faisal  |  user  |
------------------------------------------------------------

下面是我的SQL小提琴

Node Table

我要选择父节点下的所有子节点

1 个答案:

答案 0 :(得分:1)

我想通过MySQL查询选择Floor-1的所有子节点

我想要以下输出

select * from mdc_node;

SELECT t1.id, t1.root, t1.lft, t1.rgt, t1.lvl, t1.name, t1.icon
-- from 1st copy of a table
FROM mdc_node t1
-- join 2nd copy of a table used for to get the info about needed parent
JOIN mdc_node t2
-- child nodes left and right are between left and right of their  parent
                ON t1.lft BETWEEN t2.lft AND t2.rgt
-- we need only the next level
                AND t1.lvl = t2.lvl + 1
-- specify parent
WHERE t2.name = 'Floor-1';

fiddle

唯一的问题-我无法理解为什么行icon的{​​{1}}在输出中是id=3而在源数据中是'folder'

相关问题