
March 17th, 2005, 07:09 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 1
Time spent in forums: 17 m
Reputation Power: 0
|
|
Counting Foreign Keys in more than one table
Hi,
I'm hoping someone can help me out with this. It should be a simple thing to do, but I'm no SQL guru and I just can't figure it out...
I want to do a select and include in it the number of matching child records there are in other tables.
Say I have a table of courses and a table also to map users to those courses. Also I have a second table with feedback from the courses, which maps anonymously to the main courses table.
I want to do a SELECT and include columns for the number of people on a course, and the number of feedbacks received (ie. in each of two child tables, count the number of matching foreign keys to the courses primary key).
I've managed to do the first of these like this... (any improvement suggestions welcome!!)
SELECT courses.id, name, org, courses.status, COUNT(courseXuser.courseid) AS num_people FROM courses LEFT JOIN courseXuser ON (courses.id = courseXuser.courseid) GROUP BY courses.id
... but I can't figure out how to also select the number of matching foreign keys in the feedback table too. Just repeating the JOIN and COUNTing doesn't work.
Help!!!
TIA,
Moogi.
|