显示结果未加入 table
Show Results not in joining table
我有三个 table:
- child仁
- 活动
- 注册Activity
Table 'children' 包含 children.
的详细信息
Table 'activities' 包含 activity.
的详细信息
当 child 注册 activity 时,一个条目将添加到 table 中,其 activityID 和 childID。我正在尝试获取尚未注册 activity 的 children 的列表。我尝试了以下查询,但出现错误:
"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
记录集还需要包括基于在 table 中加入 'section' 的结果 Children 和 Activity。 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
SELECT
*
FROM
Childeren C1
WHERE
NOT EXISTS (SELECT *
FROM
signupActivity S1
WHERE
S1.activityID= 8 AND
C1.ChildID= S1.ChildID)
我会使用 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
就我个人而言,我不喜欢在每个列名称中重复 table 名称。我发现使用它既没有必要又令人沮丧。您的查询可以很简单:
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
SELECT *
FROM `children` c
WHERE c.childID NOT IN(
SELECT `childID`
FROM `activities`);
我有三个 table:
- child仁
- 活动
- 注册Activity
Table 'children' 包含 children.
的详细信息Table 'activities' 包含 activity.
的详细信息当 child 注册 activity 时,一个条目将添加到 table 中,其 activityID 和 childID。我正在尝试获取尚未注册 activity 的 children 的列表。我尝试了以下查询,但出现错误:
"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
记录集还需要包括基于在 table 中加入 'section' 的结果 Children 和 Activity。 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
SELECT
*
FROM
Childeren C1
WHERE
NOT EXISTS (SELECT *
FROM
signupActivity S1
WHERE
S1.activityID= 8 AND
C1.ChildID= S1.ChildID)
我会使用 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
就我个人而言,我不喜欢在每个列名称中重复 table 名称。我发现使用它既没有必要又令人沮丧。您的查询可以很简单:
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
SELECT *
FROM `children` c
WHERE c.childID NOT IN(
SELECT `childID`
FROM `activities`);