我正在维护OLD sql数据库,并且我有一个select子句:
SELECT
[P].[ItemID]
, SUM([p].[OnOrder]) AS [OnOrder]
, [I].[FullName] AS [PartNo]
, [I].[PurchaseDesc] AS [Description]
, [I].[ItemCust3]
FROM @POItems AS [P]
INNER JOIN [tblMakeUseType] AS [MUT] WITH(NOLOCK)
INNER JOIN [ItemInventory] AS [I] WITH(NOLOCK) ON [MUT].[MakeUseTypeID] = [I].[MakeUseTypeID] ON [P].[ItemID] = [I].[NCItemInventoryID]
如您所见,第一个INNER JOIN
没有ON
子句,但是在第二个内部联接之后,第一个ON
有INNER JOIN
子句,我从没见过,有什么方法可以进行反向工程并将其用作典型的INNER JOIN
?我的意思是每个人都带有他的ON
子句?问候
答案 0 :(得分:2)
您的查询被解释为:
FROM @POItems [P] INNER JOIN
([tblMakeUseType] [MUT] INNER JOIN
[ItemInventory] [I]
ON [MUT].[MakeUseTypeID] = [I].[MakeUseTypeID]
)
ON [P].[ItemID] = [I].[NCItemInventoryID]
很好。但是您可以将其更常规地编写为:
FROM [tblMakeUseType] [MUT] INNER JOIN
[ItemInventory] [I]
ON [MUT].[MakeUseTypeID] = [I].[MakeUseTypeID] INNER JOIN
@POItems [P]
ON [P].[ItemID] = [I].[NCItemInventoryID]
或者:
FROM @POItems [P] INNER JOIN
[ItemInventory] [I]
ON [P].[ItemID] = [I].[NCItemInventoryID] INNER JOIN
[tblMakeUseType] [MUT]
ON [MUT].[MakeUseTypeID] = [I].[MakeUseTypeID]
对于表 inner 来说,表的顺序无关紧要,尽管必须先声明表的别名才能用于ON
条件。