0

Prompt how to implement a recursive problem to Mysql:

I have a table:

CREATE TABLE t_info(id INT PRIMARY KEY, parent_id INT, info VARCHAR(32));

Need to get info by id of ID row and info of parent of ID and info of parent of parent of ID etc until the first parent.

How to do it in one SQL query?

Zhihar
  • 1,306
  • 1
  • 22
  • 45
  • @KannanKandasamy the dupe ref is getting children. Here parents are sought – Drew Sep 26 '16 at 19:56
  • With the reverse join can we not get parents? – Kannan Kandasamy Sep 26 '16 at 19:58
  • Lots of helpful information here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ – Barmar Sep 26 '16 at 20:03
  • @KannanKandasamy if you have the op schema (ref to parent) and you can find a dupe target that solves it, sure, ping me, will close it – Drew Sep 26 '16 at 20:05
  • @hogan that gets children. Op wants parents on up – Drew Sep 26 '16 at 20:10
  • @Drew -- there is no difference – Hogan Sep 26 '16 at 20:19
  • @Hogan except that they are going in a different direction and the question is different – Drew Sep 26 '16 at 20:20
  • @drew you are joining to the parent or you are joining to the child -- there is no "direction" in relational databases.... except when they go south :D – Hogan Sep 26 '16 at 21:01
  • @Hogan the answer, call it the one with 46 votes, finds Electronics on down to walkie-talkies. It won't go up to find higher level categories above Electronics. So it is not a solution. Cheers – Drew Sep 26 '16 at 21:12
  • @Drew -- take that solution and replace every instance of parent with child and child with parent and then it will go up. Please don't be so unaware of data structures this is not obvious. – Hogan Sep 27 '16 at 13:58
  • When you dupe close point out the discrepancy to the op. Data is my thing so I am quite aware of data structures. – Drew Sep 27 '16 at 15:06

0 Answers0