I'm trying to wrap my head around querying a table that has hierarchical category data (used for a cms) which is also tied to my posts data and a many-to-many type relationship with my post2cat table. Specifically, my question is how can I get all posts that belong to any of the subcategories (not limited to direct descendant but can be n levels deep) of a specific category id? Here are my tables:
'categories' table:
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| parent_id | int(11) | YES | MUL | 0 | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
'post2cat' table:
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| post_id | int(11) | NO | MUL | NULL | |
| cat_id | int(11) | NO | MUL | NULL | |
+---------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
'posts' table:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(256) | NO | | NULL | |
| content | text | NO | | NULL | |
| end_date | datetime | NO | | NULL | |
| format_id | int(11) | NO | | NULL | |
| featured | int(1) | NO | | NULL | |
| status | int(3) | NO | | NULL | |
| publish_date | datetime | NO | | NULL | |
| date_created | datetime | NO | | NULL | |
| date_modified | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
post_id name description
post2cat