This problem is a strange one and I think I am simply just thinking about this wrong. I am trying to get (recursively) all page URL's associated with a given contractor. The three relevant tables are:
|-------------------------------------------------|
| client_sections |
|-------------------------------------------------|
| contractor_id | reusable_section_id |
|-------------------------------------------------|
|--------------------------------------------------------|
| reusable_sections |
|--------------------------------------------------------|
| reusable_category_id | reusable_section_id |
|--------------------------------------------------------|
|--------------------------------------------|
| reusable_content |
|--------------------------------------------|
| reusable_section_id | page_url |
|--------------------------------------------|
Now here is the need -- The first query is
SELECT reusable_section_id FROM client_sections where contractor_id = '119'
This gets all the top level "parent sections" -- Which is reusable_category_id
in the table reusable_sections
. If I had to stop there I would have it handled. But the caveat that I am running into is that each of those returned sections "could" be a parent section as well. I have tried a nested SELECT
and the result was nothing short of disastrous!
What I am attempting:
» Get contractor parent sections by contractor ID from client_sections table
» get sections by reusable_category_id = reusable_section_id in reusable_sections --
» get page_url for all the sections returned (including the initial parents)
by reusable_section_id in reusable_content -- I am good until this point
» recur the last two steps with the current reusable_section_id as
reusable_category_id to check for children (!!#$@#!!!)
» Finally return all pages for all sections under that contractor_id
I have attempted queries ranging from crazy JOINS
s to nested SELECT
s to no avail. I have read many posts including This One. I am a competent SQL developer, I think my logic is flawed and a simple point in the right direction may just spark my creativity! Is the best way a WHILE
loop? Nested SELECT
? JOIN
? My brain is fried! Help?