我现在正在维护别人的 SQL,我在存储过程中遇到了这个问题:
SELECT
Location.ID,
Location.Location,
COUNT(Person.ID) As CountAdultMales
FROM
Transactions INNER JOIN
Location ON Transactions.FKLocationID = Location.ID INNER JOIN
Person ON Transactions.FKPersonID = Person.ID
AND DATEDIFF(YEAR, Person.DateOfBirth, GETDATE()) >= 18 AND Person.Gender = 1
WHERE
((Transactions.Deleted = 0) AND
(Person.Deleted = 0) AND
(Location.Deleted = 0))
上面和这个有什么区别吗(我会这样写)
SELECT
Location.ID,
Location.Location,
COUNT(Person.ID) As CountAdultMales
FROM
Transactions INNER JOIN
Location ON Transactions.FKLocationID = Location.ID INNER JOIN
Person ON Transactions.FKPersonID = Person.ID
WHERE
((Transactions.Deleted = 0) AND
(Person.Deleted = 0) AND
(Location.Deleted = 0) AND
(DATEDIFF(YEAR, Person.DateOfBirth, GETDATE()) >= 18) AND
(Person.Gender = 1))
就个人而言,我发现将条件放在 WHERE 子句中最易读,但我想知道是否有性能或其他原因来“条件化”(如果有这样的话)JOIN
谢谢