我通常非常熟悉 JOINS,但这是新的。
假设三个表(两个表和第三个链接器表的经典情况):
Customer Product Transaction -------- ------- ----------- ID ID CustomerID Name Desc ProductID Cost Date
(Simplistic on purpose, I can't reproduce the actual structure, it's not my code.)
Normally, to get a table of "who bought what when", I'd do this:
SELECT Customer.Name, Product.Desc, Transaction.Date
FROM Product
INNER JOIN Transaction ON Transaction.ProductID = Product.ID
INNER JOIN Customer ON Transaction.CustomerID = Customer.ID
但我已经看到了这个:
SELECT Customer.Name, Product.Desc, Transaction.Date
FROM Product
INNER JOIN ( Transaction
INNER JOIN Customer ON Transaction.CustomerID = Customer.ID)
ON Transaction.ProductID = Product.ID
这是什么?只是另一种语法,还是性能技巧?
(它在 SQLServer 上,仅供引用,但大概可以应用于其他人......)
最佳答案
括号不会改变语义。 ON
的位置子句控制连接的逻辑处理顺序。
第一个查询
SELECT Customer.Name,
Product.Desc,
Transaction.Date
FROM Product
INNER JOIN Transaction
ON Transaction.ProductID = Product.ID
INNER JOIN Customer
ON Transaction.CustomerID = Customer.ID
第二次查询
(删除了多余的括号)
SELECT Customer.Name,
Product.Desc,
Transaction.Date
FROM Product
INNER JOIN Transaction
INNER JOIN Customer
ON Transaction.CustomerID = Customer.ID
ON Transaction.ProductID = Product.ID
所以逻辑上在您的第一个示例中,加入
Transaction, Product
首先发生,然后将由此产生的虚拟表连接到 Customer
,而在你的第二个例子中加入 Transaction, Customer
首先发生然后由此产生的虚拟表连接到 Product
这只是逻辑上的,因为内连接既是关联又是可交换的,这可能不会对执行计划产生任何影响(除非您将
OPTION (FORCE ORDER)
添加到查询中),但它可以用于外连接。这是 covered by Itzik Ben Gan here但文章有不少不准确之处,见follow up letter by Lubor Kollar以及。
关于sql - 奇数 INNER JOIN 语法和封装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33964177/