SQL - 内连接
SQL - Inner join
我有一个查询如下:
SELECT t1.onderzoeksNR, t1.antwoorden as b1_m1_v1_items, t1.commentaar as b1_m1_v1_toe, t2.antwoorden as b1_m1_v2_items, t2.commentaar as b1_m1_v2_toe
FROM antwoorden t1
INNER JOIN antwoorden t2
ON t1.onderzoeksNR = t2.onderzoeksNR
WHERE t1.behandeltraject = 1 AND t1.meting = 1 AND t1.vragenlijst = 1
AND t2.behandeltraject = 1 AND t2.meting = 1 AND t2.vragenlijst = 2
这将输出的是:
这正是我想要的。
但是,问题是这样的:
如果我再添加一个内连接
inner join antwoorden t3
on t1.onderzoeksNR = t3.onderzoeksNR
然后喜欢:
t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
查询将返回 0 条记录。
显然,这是因为在 antwoorden
中 没有行 包含所询问的属性。
(t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
)
我的问题:
怎么还能找回当初的东西,
然后向其添加内部联接 antwoorden t3
,但随后使用空值(如果未找到包含 t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
的行)?
为了说明我的意思:
而不是返回 0 条记录。
如有任何帮助,我们将不胜感激
你应该在最后一个中使用 LEFT OUTER JOIN
而不是 INNER JOIN
:
LEFT JOIN antwoorden t3 ON t1.onderzoeksNR = t3.onderzoeksNR
您需要一个 LEFT JOIN
并且您需要将一些 WHERE
条件移动到 JOIN
条件中:
SELECT t1.onderzoeksNR, t1.antwoorden as b1_m1_v1_items, t1.commentaar as b1_m1_v1_toe, t2.antwoorden as b1_m1_v2_items, t2.commentaar as b1_m1_v2_toe
FROM antwoorden t1
INNER JOIN antwoorden t2
ON t1.onderzoeksNR = t2.onderzoeksNR
LEFT JOIN antwoorden t3 ON t1.onderzoeksNR = t3.onderzoeksNR
AND t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
WHERE t1.behandeltraject = 1 AND t1.meting = 1 AND t1.vragenlijst = 1
AND t2.behandeltraject = 1 AND t2.meting = 1 AND t2.vragenlijst = 2
LEFT JOIN
将允许您的前两个表中的记录达到 return,而不管它们是否 JOIN
到 t3
,但是如果您使用 WHERE
条件来过滤 t3.meting = 2
等处的记录,您将删除结果。将 WHERE
条件移动到 JOIN
中将 return 与您的初始查询相同的记录,只是 NULL
您添加的 t3
中的每一列,因为 none符合条件
我有一个查询如下:
SELECT t1.onderzoeksNR, t1.antwoorden as b1_m1_v1_items, t1.commentaar as b1_m1_v1_toe, t2.antwoorden as b1_m1_v2_items, t2.commentaar as b1_m1_v2_toe
FROM antwoorden t1
INNER JOIN antwoorden t2
ON t1.onderzoeksNR = t2.onderzoeksNR
WHERE t1.behandeltraject = 1 AND t1.meting = 1 AND t1.vragenlijst = 1
AND t2.behandeltraject = 1 AND t2.meting = 1 AND t2.vragenlijst = 2
这将输出的是:
这正是我想要的。
但是,问题是这样的:
如果我再添加一个内连接
inner join antwoorden t3
on t1.onderzoeksNR = t3.onderzoeksNR
然后喜欢:
t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
查询将返回 0 条记录。
显然,这是因为在 antwoorden
中 没有行 包含所询问的属性。
(t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
)
我的问题:
怎么还能找回当初的东西,
然后向其添加内部联接
antwoorden t3
,但随后使用空值(如果未找到包含t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
的行)?
为了说明我的意思:
而不是返回 0 条记录。
如有任何帮助,我们将不胜感激
你应该在最后一个中使用 LEFT OUTER JOIN
而不是 INNER JOIN
:
LEFT JOIN antwoorden t3 ON t1.onderzoeksNR = t3.onderzoeksNR
您需要一个 LEFT JOIN
并且您需要将一些 WHERE
条件移动到 JOIN
条件中:
SELECT t1.onderzoeksNR, t1.antwoorden as b1_m1_v1_items, t1.commentaar as b1_m1_v1_toe, t2.antwoorden as b1_m1_v2_items, t2.commentaar as b1_m1_v2_toe
FROM antwoorden t1
INNER JOIN antwoorden t2
ON t1.onderzoeksNR = t2.onderzoeksNR
LEFT JOIN antwoorden t3 ON t1.onderzoeksNR = t3.onderzoeksNR
AND t3.behandeltraject = 1 AND t3.meting = 2 AND t3.vragenlijst = 1
WHERE t1.behandeltraject = 1 AND t1.meting = 1 AND t1.vragenlijst = 1
AND t2.behandeltraject = 1 AND t2.meting = 1 AND t2.vragenlijst = 2
LEFT JOIN
将允许您的前两个表中的记录达到 return,而不管它们是否 JOIN
到 t3
,但是如果您使用 WHERE
条件来过滤 t3.meting = 2
等处的记录,您将删除结果。将 WHERE
条件移动到 JOIN
中将 return 与您的初始查询相同的记录,只是 NULL
您添加的 t3
中的每一列,因为 none符合条件