I have three tables:
Table 'children' contains details of children.
Table 'activities' contains details of an activity.
When a child signs up for an activity an entry is added to the table with the activityID and childID. I am trying to get a list of children that haven't signed up to an activity. I have tried the following query but I am getting an error:
"Not unique table/alias: 'children'"
SELECT children.childrenEmailAddress
FROM children
INNER JOIN activities ON signupActivity.SignupActivityID = activities.activityID
INNER JOIN children ON signupActivity.signupActivitychildID = children.childrenID
LEFT JOIN signupActivity ON children.childrenID = signupActivity.signupActivitychildID
WHERE activities.activityID = 8
The recordset would also need to include a result based on joining a 'section' in the tables Children and Activity. children.childrenSection = activities.activitySection as well as filtering the recordset by activities.activityID
This was the final that I adapted from Arth:
SELECT c.childrenEmailAddress
FROM children c
JOIN activities a
ON a.activitySection = c.childrenSection
AND a.activityID = 8
LEFT JOIN signupActivity sa
ON sa.signupActivitychildID = c.childrenID
WHERE sa.signupActivitychildID IS NULL
I'd use a LEFT JOIN
and a NULL
check
SELECT c.childrenEmailAddress
FROM children c
LEFT JOIN signupActivity sa
ON sa.signupActivitychildID = c.childrenID
AND sa.SignupActivityID = 8
WHERE sa.signupActivitychildID IS NULL
Personally I'm not a fan of repeating the table name in each of the column names.. I find it unnecessary and frustrating to work with. Your query could be as simple as:
SELECT c.emailAddress
FROM child c
LEFT JOIN signupActivity sa
ON sa.childId = c.id
AND sa.activityID = 8
WHERE sa.childId IS NULL
UPDATE
SELECT c.childrenEmailAddress
FROM children c
JOIN activities a
ON a.activitySection = c.childrenSection
AND a.activityID = 8
LEFT JOIN signupActivity sa
ON sa.signupActivitychildID = c.childrenID
AND sa.signupActivityactivityID = a.activityID
WHERE sa.signupActivitychildID IS NULL