In the most general of cases, I have a query like below:
SELECT tutor_school.name, count(*), tutor_school.schoolid
FROM tutor_school, tutor_attends, tutor_tutors_in
WHERE
tutor_school.schoolid = tutor_attends.schoolid and
tutor_school.schoolid in ('1', '2', '3') and
tutor_attends.userid=tutor_tutors_in.userid
group by tutor_school.schoolid LIMIT 0, 10
In essence, I want:
Name of School, Number of Students attending that school that tutor in any subject, Schoolid
What I'm actually getting is
Name of School, sum of all subjects taught by students at that school, schoolid -- in other words, if student 1 tutors 3 subjects, student 2 tutors 5, then instead of returning 2 I get 8!
I've realized that the issue is with the following statement:
tutor_attends.userid=tutor_tutors_in.userid
This isn't checking the existence of a given foreign key in a remote table, it's giving a result for each instance of that key.
What I'm trying to figure out is how to bind it to limit it to simply the existence of the given key, not the number of times that key occurs. I know I've seen a case similar to this in my SQL class, but I can't remember what the solution was.