0

I have a table representing the family tree. It has fields ID, NAME, LEVEL, PARENT_ID.

enter image description here

And the column PARENT_ID is the foreign key and refers to column ID of this table. How can I get a list of all (any level to the bottom of the hierarchy) children knowing only the ID of specific person's row?

sqlfiddle.com

I need to get it in a single query without using a stored procedure calls. Please help make this the most well.

For example, if ID is 7 then result should be:

enter image description here

I use MySQL-5.1.

Edit: If the original issue has not solution, maybe is there solution when maximum level equal to 5 ?

Mike Brown
  • 201
  • 3
  • 11
  • I don't think you can. Since there are an indefinite number of levels to the bottom, there is no one SQL statement to go all the way to the bottom. – Marlin Pierce Jun 16 '14 at 15:03
  • possible duplicate of [Get all child, grandchild etc nodes under parent using php with mysql query results](http://stackoverflow.com/questions/11497202/get-all-child-grandchild-etc-nodes-under-parent-using-php-with-mysql-query-resu) – Michael Kunst Jun 16 '14 at 15:03
  • I do not need to use PHP, I need SQL – Mike Brown Jun 16 '14 at 15:07
  • 1
    mysql doesn't support recursive queries. you can do n-way self joins to get get back "up" n levels, but it's not a general solution. The usual workaround is to just to do queries in a loop and work your way back up. – Marc B Jun 16 '14 at 15:16
  • If the original issue has not solution, maybe is there solution when maximum level equal to 5 ? – Mike Brown Jun 16 '14 at 15:18
  • Actually, I was tinkering with this just the other day, and have a solution. need some time to draw up though. – Jaaz Cole Jun 16 '14 at 16:51

1 Answers1

2

EDIT: NOT a Complete solution. The execution trick only follows one of many branches, and fails to deliver more than one grandchild branch. Per Comments. Seen in the results on SQLFiddle, as well.

So, the solution here at SQLFiddle illustrates an interesting execution hack in MySQL that can be taken advantage of.

SELECT 
  @parent_id := id AS id,
  parent_id,
  name,
  level,
  @depth := @depth + 1 AS depth
FROM 
  family_tree t
  join (SELECT @parent_id := 7, @depth := 0) f
WHERE id = @parent_id or parent_id = @parent_id ;

Essentially, declare your @parent_id variable as the root you start with. The join subquery is only executed once. Then, when each row redefines @parent_id, the where clause is reevaluated, and returns a new row, which redefines @parent_id, and the loop continues until there are no more rows.

Jaaz Cole
  • 3,119
  • 1
  • 15
  • 20
  • So this is not a complete solution. It will only give you one part of the tree... if there are more than one grandchild they will be excluded out of the tree. nice query though, I did something similar to this a few months ago :) – John Ruddell Jun 17 '14 at 17:05
  • Huh, That is true. I'm trying to translate my TSQL to MySQL, and it would appear that I trip occasionally. Thanks for pointing that out. – Jaaz Cole Jun 17 '14 at 17:13
  • No prob :) I think the best way to go about this would be to make a user-defined variable with a list of id's that you can compare off of.. thats what I had done previously to make a complete tree :) – John Ruddell Jun 17 '14 at 17:16
  • I'd love to see your solution, if you care to post the answer, or a link to it? I have a recursive self-relationship project coming up in MySQL that it would be extremely helpful for. – Jaaz Cole Jun 17 '14 at 17:23