0

I am new to sql - trying to learn. I have a specific use case and need help with the same.

I have 2 tables and i need to run a query to join those and get the data back.

Table A Parent Item Child Item Relationship Type

Table B Item Name Item ID Item Color

So, the data is in such a way, where items can have hierarchical relationship. For eg:

Item A is a parent for Item B and Item C. Item B is a parent for Item D and Item E. Item D is a parent for Item F and Item G.

I need a query where i want to provide a Child Item and get back all the Parents until its last level in the hierarchy. For eg: If i provide Item G as the input, the output should give me a list of Items D, B and A.

I don't know if CONNECT BY is something that i can use, but need help with this.

Thank you in advance.

Srini
  • 1
  • 1
  • What SQL are you using? MySQL, SQL server, Postgres, etc.? – raphael75 Jun 30 '16 at 19:00
  • Actually - Postgresql – Srini Jun 30 '16 at 20:44
  • I'm not sure if you can do something like this directly in Postgres because I haven't used it (I use MySQL). This type of thing is usually done with a server-side scripting language like PHP in a loop that returns the parent of each child. This thread shows a way to do it in MySQL: http://stackoverflow.com/questions/12948009/finding-all-parents-in-mysql-table-with-single-query-recursive-query but I don't know if there's a way to convert it to Postgres. – raphael75 Jul 01 '16 at 18:08

0 Answers0