I am using PostgreSQL 9.2. I have a table of products which stores the information about the products and its child table is content table , which contains the products linked to it.
suppose for example If I have a product called Burger in the main table, Then I will be having its content products like
bread,cheese,chilli.
Their can be a scenario where bread will be main product and its content will be flour and salt etc.
I am writing a query to retrieve all the products(ids associated with them) with their content products which should display something like this hierarchy
Burger----bread--+----flour
+----salt
----cheese
----chilli
I have to get the result like this
burger bread
burger cheese
burger chilli
bread flour
bread salt
This hierarchy can run up to n level (like even floor can have sub content with it in that case it should something like this
burger bread
burger cheese
burger chilli
bread flour
bread salt
flour someprod1
flour someprod2 assuming if someprod1 and someprod2 are the contents)
I have written the following query:
select rec.m_product_id,rec.m_productbom_id
from rec_product_recipe rec
join rec_product_recipe rec1
on rec1.m_productbom_id = rec.m_product_id
But this shows up to one level itself as:
burger bread
burger cheese
burger chilli