I have a table like this:
--------------------Categories-------------------
| Category | Label | Parent |
*************************************************
| / | Home | NULL |
-------------------------------------------------
| leasing | Leasing | / |
-------------------------------------------------
| leasing-steps | Leasing Steps | leasing |
-------------------------------------------------
| new-tenant | New Tenants | leasing-steps |
-------------------------------------------------
| properties | Properties | leasing |
-------------------------------------------------
| about | About Us | / |
-------------------------------------------------
I receive a variable (category) from PHP, let's say the value is 'leasing-steps'
.
I need to come up with an SQL query that will be able to take any category and give me it's hierarchy, in the proper order. For this example ('leasing-steps'
), it would have to output this:
---Categories---
| Category |
*****************
| / |
-----------------
| leasing |
-----------------
| leasing-steps |
-----------------
I tried this:
SELECT `Cat1`.`Category`
FROM Categories AS `Cat1`
LEFT JOIN Categories AS `Cat2` ON `Cat1`.`Parent`
WHERE `Cat1`.`Category` = 'leasing-steps'
But it only gives me 1 entry and not the desired result.
Keep in mind that I am working with an unknown number of parents. it could be 1 level or 99999