我有三张桌子:
表'儿童'包含儿童的详细信息。
表'活动'包含活动的详细信息。
当孩子注册活动时,会在表中添加一个带有activityID和childID的条目。我正在尝试获取尚未注册活动的孩子列表。我尝试了以下查询,但收到错误:
“不唯一的表/别名:'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
记录集还需要包含基于加入表Children和Activity中的'section'的结果。 children.childrenSection = activities.activitySection以及通过activities.activityID过滤记录集
这是我改编自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
答案 0 :(得分:1)
SELECT
*
FROM
Childeren C1
WHERE
NOT EXISTS (SELECT *
FROM
signupActivity S1
WHERE
S1.activityID= 8 AND
C1.ChildID= S1.ChildID)
答案 1 :(得分:1)
我使用LEFT JOIN
和NULL
支票
SELECT c.childrenEmailAddress
FROM children c
LEFT JOIN signupActivity sa
ON sa.signupActivitychildID = c.childrenID
AND sa.SignupActivityID = 8
WHERE sa.signupActivitychildID IS NULL
就我个人而言,我不喜欢在每个列名中重复表名。我发现使用它是不必要和令人沮丧的。您的查询可以简单如下:
SELECT c.emailAddress
FROM child c
LEFT JOIN signupActivity sa
ON sa.childId = c.id
AND sa.activityID = 8
WHERE sa.childId IS NULL
<强>更新强>
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
答案 2 :(得分:0)
SELECT *
FROM `children` c
WHERE c.childID NOT IN(
SELECT `childID`
FROM `activities`);