I have a company
table that references itself, like this:
company_id parent_id
-----------------------
1 (NULL)
2 1
3 2
4 1
5 (NULL)
So a company
can have a parent company
and so forth.
I also have a lookup table to connect a customer
to their related company
's, like such:
customer_id company_id
--------------------------
1 1
1 3
1 4
2 3
2 2
I'm trying to create a SQL call that will get a list of distinct company_id's that are related to a customer_id. This means I need to get the company ids of not just the one to one relationships from the lookup table, but also run through all the parents, add the parent ids if they aren't already in the list, then look at the parents' parents, add their ids if they aren't already in the list, and so forth.
So for example, in the tables above if I were to try to find all the company_id's where customer_id = 2
, I would expect to get back:
company_id
---------------
1
2
3
Notice it got 1 recursively, but it did not list 2 twice because it's only looking for distinct id's.
I'm pretty lost when it comes to recursive SQL calls though. Is this possible or should I do this in code?