在SQL

时间:2016-09-03 15:23:03

标签: sql-server join right-to-left right-join

我有这些疑问:

SELECT        
    dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, 
    dbo.BaseMaterials.Name AS MaterialName, 
    dbo.MaterialDescriptions.Name AS MaterialDescription, 
    dbo.MaterialDescriptions.Description, 
    dbo.MaterialScopes.ScopeName, dbo.MaterialScopeObjectNames.ObjectName, 
    dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, 
    dbo.MaterialDescriptions.ItemCode, dbo.Materials.Quantity, 
    dbo.Materials.Discipline, dbo.Materials.Id, dbo.Lines.Id AS LineId
FROM
    dbo.Materials 
INNER JOIN
    dbo.Lines ON dbo.Materials.LineId = dbo.Lines.Id 
INNER JOIN
    dbo.BaseMaterials ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id 
INNER JOIN
    dbo.MaterialDescriptions ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id 
INNER JOIN
    dbo.MaterialScopes ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id 
INNER JOIN
    dbo.MaterialScopeObjectNames ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id

它返回16000条记录。我有另一个名为joints的表,与material上的lineId表有关系,每个material可以有multi joints所以当我添加{{1}时我的查询表就像这样:

joints

正如您在最后一行中所看到的,我在SELECT dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, dbo.BaseMaterials.Name AS MaterialName, dbo.MaterialDescriptions.Name AS MaterialDescription, dbo.MaterialDescriptions.Description, dbo.MaterialScopes.ScopeName, dbo.MaterialScopeObjectNames.ObjectName, dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, dbo.MaterialDescriptions.ItemCode, dbo.Materials.Quantity, dbo.Materials.Discipline, dbo.Materials.Id, dbo.Lines.Id AS LineId, dbo.Joints.TestPackageId FROM dbo.Materials INNER JOIN dbo.Lines ON dbo.Materials.LineId = dbo.Lines.Id INNER JOIN dbo.BaseMaterials ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id INNER JOIN dbo.MaterialDescriptions ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id INNER JOIN dbo.MaterialScopes ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id INNER JOIN dbo.MaterialScopeObjectNames ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id INNER JOIN dbo.Joints ON dbo.Materials.LineId = dbo.Joints.LineId joints之间进行了联接,以访问materials表中的testpackageid列。但我的结果返回220000记录。如何更改联接类型,我的意思是jointsleft,只返回right

16000条记录

2 个答案:

答案 0 :(得分:2)

您无法使用JOIN。但是,您可以使用CROSS APPLY

FROM . . .
     dbo.MaterialScopeObjectNames
     ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id CROSS APPLY
     (SELECT TOP 1 j.*
      FROM dbo.Joints j
      WHERE dbo.Materials.LineId = j.LineId
     ) J

通常情况下,使用ORDER BY时会包含TOP,因此您可以控制所返回的行。

另请注意,使用表别名可以使查询更容易编写和读取。

答案 1 :(得分:1)

只需子查询就可以轻松获得它。保持查询不变,并添加子查询以从关节中获取该信息。

SELECT        dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, dbo.BaseMaterials.Name AS MaterialName, dbo.MaterialDescriptions.Name AS MaterialDescription, dbo.MaterialDescriptions.Description, 
                         dbo.MaterialScopes.ScopeName, dbo.MaterialScopeObjectNames.ObjectName, dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, dbo.MaterialDescriptions.ItemCode, dbo.Materials.Quantity, 
                         dbo.Materials.Discipline, dbo.Materials.Id, dbo.Lines.Id AS LineId,
                         (select top 1 dbo.Joints.TestPackageId from dbo.Joints where dbo.Joints.LineId = dbo.Materials.LineId)
FROM            dbo.Materials INNER JOIN
                         dbo.Lines ON dbo.Materials.LineId = dbo.Lines.Id INNER JOIN
                         dbo.BaseMaterials ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id INNER JOIN
                         dbo.MaterialDescriptions ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id INNER JOIN
                         dbo.MaterialScopes ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id INNER JOIN
                         dbo.MaterialScopeObjectNames ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id