I have a mysql table called content which stores content data for a content management system.
NOTE: all content is organised into a hierachy using a parent id column.
+----+------------+-----------------+--------+
| id | slug | content_type_id | parent |
+----+------------+-----------------+--------+
| 1 | portfolio | 5 | 0 |
| 2 | about-us | 1 | 0 |
| 3 | find-us | 1 | 0 |
| 4 | contact-us | 1 | 2 |
| 5 | find-us | 1 | 4 |
+----+------------+-----------------+--------+
I need a query to select the correct row in the table depending on what the slug name is. The problem is when slugs have the same name.
I have two possible paths, which a user can visit:
/find-us/
and
/about-us/contact-us/find-us/
I can think of one solution:
Which is to create an another column with the full paths:
full_path
--------
/portfolio/
/about-us/
/find-us/
/about-us/contact-us/
/about-us/contact-us/find-us/
But are there any kind of clever methods I can use to select the correct row. I am not sure if creating another column with full path names is such a great idea (because these have the potential to change), personally I would only like to use that as a last resort.
Thanks.
This would be possible if your DBMS supported recursive queries:
Output: