I have the following tables in a database:
- LIST: which contains metadata of some list.
- LISTITEM: which is a single list entry, linking a thing as ObjectID, and links back to a LIST by referencing the ListID.
- OBJECT: An actual thing that is to be included in these lists. These are what we commonly reference in the LISTITEM to be listed.
However, in addition to OBJECT, another LIST can also be referenced in a LISTITEM, creating a hierarchy of nested lists within each other.
My goal is to query all FOOs that contain BAR Objects that are nested within a hierarchy of linked LISTs.
My sample code so far:
SELECT F.ID, F.Name
FROM Foo F
LEFT JOIN List L ON L.ID = F.ListID
LEFT JOIN ListItem LI ON LI.ListID = L.ID
LEFT JOIN List L2 ON L2.ID = LI.ChildListID
LEFT JOIN ListItem LI2 ON LI2.ListID = L2.ID
LEFT JOIN Object O ON O.ID in (LI.ObjectID, LI2.ObjectID)
WHERE O.Name like 'Bar'
However, this only will search 2 levels deep in the hierarchy and I would have to configure my WHERE statements to compare every level of Object O I create.
How can I fix up my query to be able to search within a hierarchy of N levels?