0

I have two tables:

table1 : id|name

table 2: id|parent|child

Two tables are used because an entry in table 1 can have multiple parents. So an hierarchy of sub items can exist multiple times.

For a given id in table 1, always being the lowest tree item, I would like to find the full path with all parent names up to the start of the tree.

Can this be done in a single mysql query?

Thanks!

Peter Hawthorne
  • 37
  • 1
  • 1
  • 6
  • possible duplicate of [What is the most efficient/elegant way to parse a flat table into a tree?](http://stackoverflow.com/questions/192220/what-is-the-most-efficient-elegant-way-to-parse-a-flat-table-into-a-tree) – Alex May 07 '15 at 14:06
  • This can only work if you place a limit on the number of times you want to find the parent. IE You can join table 2 back to itself via parent->id but you have to add one more join for each generation. If you limit it to, say, 3 generations then you can have 2 joins to do this, if you want 4 then 3 joins are needed etc etc – Giles May 07 '15 at 14:23
  • Thank you Alex! At first that link did seem to be like other threads I saw, parent being part of the table. But there is some useful info there! – Peter Hawthorne May 08 '15 at 07:55

0 Answers0