I have 3 tables, Subject, SubjectTeacher and Head. A business rule is each Subject has one Head.
Subject
ID Name Code
1 Mathematics Math
2 Biology Bio
3 Zoology Zoo
SubjectTeacher
TeacherID JoinDate SubjectID
1 2001-12-11 1
2 2004-12-11 2
3 2002-12-11 3
Head
TeacherID StartDate EndDate
1 2001-12-11 2016-12-11
2 2004-12-11 2014-12-11
3 2002-12-11 2017-12-11
I have 2 variables, MathHead and BioHead
I would like to write a query that populates these 2 variables
There are 2 ways I identified. One is temporary tables. Too heavy for a seemingly simple problem.
Other is multiple queries, one for each variable. This is simple/easy but as the variables increase, the queries increase too.
Is there a better way?
Update:
I am doing something like this. Also added a link table(SubjectTeacher) above
SELECT @BioHead = TeacherID
FROM Head head
JOIN SubjectTeacher st on st.TeacherID = head.TeacherID
JOIN Subject subject on st.SubjectID = subject.ID
WHERE subject.Name = 'Biology'