I have a classroom table. It has a primary key column, along with primary teacher Foreign key, secondary teacher Foreign key and tertiary teacher Foreign key. Each of the teacher FKs are keys to a user table.
A teacher could be primary in one room, secondary in another and tertiary in a third. So, I need a query that returns the Primary Key of the classroom for a teacher, based on the primary, secondary and tertiary hierarchy. For this example I would want the Primary Key of the classroom where they are the primary teacher.
If the teacher was not a primary teacher in any classroom, but was secondary teacher in one room and tertiary in a second room, then I need the Primary Key of the classroom where they are the secondary teacher.
classroom Data examples
classroom ID FKPrimary FKSecondary FKTertiary
1 100 NULL NULL
2 200 1234 400
3 300 400 500
4 400 300 200
5 500 400 4444
For teacher 100, I need classroom ID of 1.
For teacher 200, I need classroom ID of 2.
For teacher 300, I need classroom ID of 3.
For teacher 400, I need classroom ID of 4.
For teacher 500, I need classroom ID of 5.
For teacher 1234, I need classroom ID of 2.
For teacher 4444, I need classroom ID of 5.
Open in new window