I have a MySQL database which contains 2 (relevant) tables: Staff, Supervisors.
Staff contains: StaffID, Name, CostCentre
and Supervisors contains: InstanceID, StaffID, SupervisorID, Obsolete
The Staff table contains all staff in the company, and the Supervisors table links one StaffID (StaffID) to another StaffID (SupervisorID). If we create a new relationship (i.e. staff A no longer reports to staff B, but instead to staff C), then we create a new record and set the original 'obsolete' flag to 'true'.
I have arranged it this way because the Supervisor:Subordinate relationships are subject to change, and are not guaranteed to be correct: We want to be able to change them and maintain an audit trail.
What I need to do is to get a recursive list of CostCentres.
What that means is, say I start with some cost centre. I get a list of all staff who are in that cost centre, and generate a list of all their subordinates. Then I get a list of all those cost centres and repeat for each of those.
I already have a number of procedures, two of which may be relevant:
CALL getCostCentre(iCostCentre) /* get a list of all staff in iCostCentre) */
CALL getSupervisees(iSupervisor) /* get a list of all staff who report to iSupervisor */