1

I'm using mySQL on phpMyAdmin, my table is

| code | name | parent |
|------|------|--------|
|  10  |node10|   5    |
|  5   |node05|   2    |

where 5 is the parent of 10 (which is also a node), I need to select for a specific node A all childs and subchilds until the last leaf, I tried to do that but I've got also 2 levels of subchilds (for example for node 2 I've got 5 and 10)

SELECT * 
FROM `composant` 
WHERE parent IN( 
    SELECT code 
    FROM `composant` 
    WHERE parent=2 OR code=2
)

so how can we get loop to get all the subnodes ?

1
  • If I understand well, you need recursivity to do that. This can be done either by (recursive) user-defined-functions (which can be done in mySQL), or by using RECURSIVE Common-Table-Expressions. AFAIK, MariaDB offers CTEs, but MySQL 5.7 does not. Check How to do the Recursive SELECT query in MySQL?
    – joanolo
    Commented Jan 5, 2017 at 20:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.