内部连接与 where 子句中 main table 的条件
Condition on main table in inner join vs in where clause
INNER JOIN
是否有任何理由在主 table 与 WHERE
子句中有条件?
INNER JOIN
中的示例:
SELECT
(various columns here from each table)
FROM dbo.MainTable AS m
INNER JOIN dbo.JohnDataRecord AS jdr
ON m.ibID = jdr.ibID
AND m.MainID = @MainId -- question here
AND jdr.SentDate IS NULL
LEFT JOIN dbo.PTable AS p1
ON jdr.RecordID = p1.RecordID
LEFT JOIN dbo.DataRecipient AS dr
ON jdr.RecipientID = dr.RecipientID
(more left joins here)
WHERE
dr.lastRecordID IS NOT NULL;
在 WHERE
子句中使用条件查询:
SELECT
(various columns here from each table)
FROM dbo.MainTable AS m
INNER JOIN dbo.JohnDataRecord AS jdr
ON m.ibID = jdr.ibID
AND jdr.SentDate IS NULL
LEFT JOIN dbo.PTable AS p1
ON jdr.RecordID = p1.RecordID
LEFT JOIN dbo.DataRecipient AS dr
ON jdr.RecipientID = dr.RecipientID
(more left joins here)
WHERE
m.MainID = @MainId -- question here
AND dr.lastRecordID IS NOT NULL;
其他更普遍的类似问题的区别,而这是特定于 SQL 服务器的。
在问题范围内,
Is there any reason on an INNER JOIN to have a condition on the main
table vs in the WHERE clause?
这是 INNER JOIN 的 STYLE 选择。
从纯粹的风格反映来看:
虽然 STYLE 没有硬性规定,但通常观察到这是一种不太常用的样式选择。例如,这通常可能会导致更具挑战性的维护,例如如果有人在哪里删除 INNER JOIN
和所有后续的 ON
子句条件,它会影响主要的 table 结果集,或者使当它是一组非常复杂的连接时,查询更难debug/understand。
可能还需要注意的是,这一行可能会放置在许多 INNER JOINS 上,进一步增加了混乱。
INNER JOIN
是否有任何理由在主 table 与 WHERE
子句中有条件?
INNER JOIN
中的示例:
SELECT
(various columns here from each table)
FROM dbo.MainTable AS m
INNER JOIN dbo.JohnDataRecord AS jdr
ON m.ibID = jdr.ibID
AND m.MainID = @MainId -- question here
AND jdr.SentDate IS NULL
LEFT JOIN dbo.PTable AS p1
ON jdr.RecordID = p1.RecordID
LEFT JOIN dbo.DataRecipient AS dr
ON jdr.RecipientID = dr.RecipientID
(more left joins here)
WHERE
dr.lastRecordID IS NOT NULL;
在 WHERE
子句中使用条件查询:
SELECT
(various columns here from each table)
FROM dbo.MainTable AS m
INNER JOIN dbo.JohnDataRecord AS jdr
ON m.ibID = jdr.ibID
AND jdr.SentDate IS NULL
LEFT JOIN dbo.PTable AS p1
ON jdr.RecordID = p1.RecordID
LEFT JOIN dbo.DataRecipient AS dr
ON jdr.RecipientID = dr.RecipientID
(more left joins here)
WHERE
m.MainID = @MainId -- question here
AND dr.lastRecordID IS NOT NULL;
其他更普遍的类似问题的区别,而这是特定于 SQL 服务器的。
在问题范围内,
Is there any reason on an INNER JOIN to have a condition on the main table vs in the WHERE clause?
这是 INNER JOIN 的 STYLE 选择。
从纯粹的风格反映来看:
虽然 STYLE 没有硬性规定,但通常观察到这是一种不太常用的样式选择。例如,这通常可能会导致更具挑战性的维护,例如如果有人在哪里删除 INNER JOIN
和所有后续的 ON
子句条件,它会影响主要的 table 结果集,或者使当它是一组非常复杂的连接时,查询更难debug/understand。
可能还需要注意的是,这一行可能会放置在许多 INNER JOINS 上,进一步增加了混乱。