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 */
I would create a single table containing staff and supervisors, let's call it Employee, and another table for holding relationships (each table with a delete flag field and a change date field for audit trial). The table relationship has a subordinate and a master. And a table for cost centers holding references to the employees. There are some open source CRMs around that you can download (use it maybe) and eventually learn how to structure your db. Regarding cost centers, you can learn from the table Group in standard CRMs
精彩评论