Search code examples
mysqltreeviewyii2-advanced-appkartik-v

Get all the child nodes of parent nodes


I have a treeview that is rendered from Kartik Tree Manager. Below is my tree view

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

Node Table

I want to select all the child nodes under the parent node


Solution

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

    I want the following output

    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

    The only problem - I cannot understand why icon for a row id=3 is 'folder' in output whereas it is 'user' in source data.