I'm trying to create a structure to store and read dogs pedigree using php and mysql.
I found on stackoverflow this kind of structure, and it seems to be efficient: Inbreeding-immune database structure
TABLE people (id, name, father_id, mother_id );
TABLE relatives ( person_id, ancestor_id );
There is a working example here: http://sqlfiddle.com/#!2/0bd39/10
Is it possible to retrieve simply a ordered tree or a subtree (for example 4 or 5 generations) starting by an id?
EDIT
I'm trying to get the data from using the first table...but with 4-5 generations the resulting query is very very heavy. I fear that with a large amount of information in the DB, getting the genealogy could be to much slow and unusable.
SELECT
t1.name AS lev1, t2.name as f, ff1.name as ff1, fm1.name as fm1, t3.name as m,
mf1.name as mf1, mm1.name as mm1, .......
FROM people AS t1
LEFT JOIN people AS t2 ON t2.id = t1.father_id
LEFT JOIN people AS ff1 ON ff1.id = t2.father_id
LEFT JOIN people AS fm1 ON fm1.id = t2.mother_id
...
LEFT JOIN people AS t3 ON t3.id = t1.mother_id
LEFT JOIN people AS mf1 ON mf1.id = t3.father_id
LEFT JOIN people AS mm1 ON mm1.id = t3.mother_id
...
WHERE t1.id = 6;