I have a table category which has fields and values as shown below in MYSQL database.
id name parent sort_order
1 Men null 0
2 Women null 1
3 shirt 1 0
4 salwar 2 1
Here parent is a foreign key points to the same table. In my category list page i want to print the parent hierarchy(if any) along with the category name. can i do this in a single query.
I have tried with group_concat in mysql but not able to generate the required result.
Expected Result:
1 men
2. women
3. men>shirt
4. women> salwar
select concat(table1.name,case when table2.parent is not null then concat('>', table1.name) else '' end) from table1 left join table2 on tabl1.id = table2.parent