Hope I understood your question well (it's pretty late here, and I just came from the bar), if I didn’t, just correct me and I rewrote my answer.
From the above scenario, I think there is another parent table, right?
Lets imagine that attributes are id and name. The table of children is one of you (without extra attributes).
mysql> insert into parent(name) values ('petr'),('tomas'),('richard'); mysql> insert into children(name,parent_id) values('michal',1),('tomas',1),('michal'); mysql> select parent.id,parent.name,children.name from parent left join children on parent.id = children.parent_id; +----+---------+--------+ | id | name | name | +----+---------+--------+ | 1 | petr | michal | | 1 | petr | tomas | | 2 | tomas | NULL | | 3 | richard | michal | +----+---------+--------+
To do this several times (the parent received the child who received the child, who received the child, etc.). You can accomplish this using multiple joins.
mysql> select parent.id,parent.name as Parent,children.name as Child,children2.name as Child2 from parent left join children on parent.id = children.parent_id left join children2 on children.id = children2.parent_id; +----+---------+--------+--------+ | id | Parent | Child | Child2 | +----+---------+--------+--------+ | 1 | petr | michal | NULL | | 1 | petr | tomas | dan | | 1 | petr | tomas | pavel | | 2 | tomas | NULL | NULL | | 3 | richard | michal | michal | +----+---------+--------+--------+
If I either did not answer what you asked, or you need further explanation, let me know;]
Hi,
Releis
source share