Hi,
I am trying to create an sql statement that draws from a database table with id, name and sub fields. If the sub field is = 0 it is a root category, but if the sub field is equal to one of the id fields, it is the sub of the row with that id... With this statement I want to be able to populate List/menu fields with an infinate amount of sub categories. Below is an example:
Database layout:
id name sub
1 food 0
2 fruit 1
3 apples 2
food is a root category with fruit being a sub category of food and apples being a sub category of fruit.
The code I have is below. It only gives me one level of sub categories which is very limiting.
SELECT c2.id, case when c1.name is null then c2.name else concat(c1.name, ' > ', c2.name) end as name
FROM mod_infobase_category c2 left join mod_infobase_category c1 on c2.sub=c1.id
ORDER BY name
Out puts:
fruit > apples
What I need is for it to output is:
food
food > fruit
food > fruit > apples
I think I need multiple joins in the SQL statement... anyone have any ideas?
Tim