INNER JOIN
是否有理由在主表上与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 Server特有的。
答案 0 :(得分:0)
在问题的范围内,
INNER JOIN是否有任何理由在主要上有条件 表vs在WHERE子句中?
这是INNER JOIN的STYLE选择。
虽然STYLE没有严格的规则,但通常会发现这是一种不常用的风格选择。例如,通常可能会导致更具挑战性的维护,例如,如果有人删除INNER JOIN
和所有后续ON
子句条件,则会影响主表结果集,或者使查询更加困难调试/理解什么时候它是一个非常复杂的连接集。
可能还会注意到,这条线可能被放置在许多INNER JOINS上,这进一步增加了混乱。