1

I'm trying to create sql query for family tree purpuses that loops through table according to row values of the table it self.

So I have table that has values

person id  | parent 1 | parent 2  
1          | 2        | 5  
2          | 3        | 4   
3          |          |  
4          |          |  
5          | 6        | 7   
6          |          | 5  
7          |          | 

family tree table

and I want to select all the parets from the one I'm seaching to the oldest, after that search all of their offsping and after that their parents?

Examples

search done with person 1

trincot
  • 317,000
  • 35
  • 244
  • 286
Tanterus
  • 21
  • 3
  • [search done with person 2][3] [search done with person 5][4] [3]: https://i.stack.imgur.com/6RGPA.png [4]: https://i.stack.imgur.com/DplgJ.png – Tanterus Oct 21 '17 at 12:16
  • Kindly share sample data in formatted text (not images) – zarruq Oct 21 '17 at 12:22
  • 1
    A recursive common table expression is commonly used for this type of query but unfortunately I don't think MySQL supports them. I'd look at recursive CTE support/work-arounds in MySQL. – Shawn K Oct 21 '17 at 12:29
  • Which is your version of MySql? – trincot Oct 21 '17 at 12:37
  • 1
    Your text table shows an inconsistency: 6 is a parent of 5, and 5 is a parent of 6. – trincot Oct 21 '17 at 12:43

0 Answers0