Получить все дочерние узлы родительских узлов - PullRequest
0 голосов
/ 06 августа 2020

У меня есть treeview, отображаемый из Kartik Tree Manager . Ниже представлено мое древовидное представление

enter image description here

Table

enter image description here

What I want to do?

I want to select all the child nodes of Floor-1 via MySQL query

I have tried to run a query like below

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

Output

enter image description here

Desired Output

I want the following output

------------------------------------------------------------
| `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  |
------------------------------------------------------------

Below is my SQL Fiddle

Таблица узлов

Я хочу выбрать все дочерние узлы под родительским узлом

1 Ответ

1 голос
/ 06 августа 2020

Я хочу выбрать все дочерние узлы Floor-1 с помощью MySQL запроса

Мне нужен следующий результат

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 для строки id=3 - это 'folder' на выходе, тогда как 'user' в исходных данных.

...