Hierarchy - MySQL Recursive get all child from parent

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

i have this case using recursive query on Mysql to find lv 2 and lv3 child on one table...
database structure i m using :

id name parent
1    A    0
2    B    0
3    C    0
4    D    1
5    E    1
6    F    2
7    G    2
8    H    3
9    I    3
10   J    4
11   K    4

The result i was expecting,when filtering the data ,where id=1,it will generate the result i m expecting.

id name parent
4   D     1
5   E     1
10  J     4
11  K     4

or this is the illustration. https://i.stack.imgur.com/TMhRR.png https://i.stack.imgur.com/TMhRR.png

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/,but http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/,but

Answers

SELECT *
FROM TABLENAME
WHERE PARENT = 1
UNION
SELECT * 
FROM TABLENAME
WHERE PARENT IN 
    (SELECT ID FROM TABLENAME WHERE PARENT = 1)

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/41913460/mysql-recursive-get-all-child-from-parent

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils