左加入不起作用

时间:2015-12-15 16:45:28

标签: sql sql-server ms-access

我是SQL Server初学者,因此我一直在使用MS Access创建查询,然后一直在修改SQL Server。

我创建了一个包含左连接的查询,它在Access中完美运行但是当我将SQL代码复制到SQL Server时,它没有显示空值,我无法解决原因。

我想显示所有属性值是否包含值。有人可以帮忙吗?代码如下:

SELECT DISTINCT 
    tbLease.LeaseTitle
    , tbAttributeValue.AttributeTemplateDefinitionLinkID
    , tbAttributeValue.Value
FROM 
   ((((tbBusinessUnit 
LEFT JOIN 
   tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID) 
INNER JOIN 
   tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID  = tbBuildingLinkBusinessUnit.BusinessUnitID) 
INNER JOIN 
   tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID) 
INNER JOIN 
    (tbUnitLocation 
INNER JOIN 
    tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID) 
              ON tbBuilding.BuildingID = tbUnitLocation.LocationID) 
INNER JOIN 
    tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE 
    (((tbAttributeValue.AttributeTemplateDefinitionLinkID) = 30 
      Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 31 
      Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 32));

3 个答案:

答案 0 :(得分:1)

如果没有一些例子,很难说你想要什么,但你可能想要这个:

WHERE coalesce(tbAttributeValue.AttributeTemplateDefinitionLinkID,30) in (30,31,32);

这将为您提供AttributeTemplateDefinitionLinkID为空的项目 (即它没有加入左连接)或者是这3个值中的一个。

现在,如果你没有加入左连接,由于where条件,它将不显示该行,所以你的左连接与内连接相同。

答案 1 :(得分:1)

您通常会看到左边连接的NULL值会被WHERE子句过滤掉。

每当您向适用于外部联接表的WHERE子句添加过滤器时,它将有效地使其与内部联接相同,除非包含NULL值作为选项在你的where子句中。

SELECT DISTINCT 
tbLease.LeaseTitle,
tbAttributeValue.AttributeTemplateDefinitionLinkID,
tbAttributeValue.Value

FROM 
tbBusinessUnit 
LEFT JOIN tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID 
INNER JOIN tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID  = tbBuildingLinkBusinessUnit.BusinessUnitID 
INNER JOIN tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID
INNER JOIN tbUnitLocation ON tbBuilding.BuildingID = tbUnitLocation.LocationID
INNER JOIN tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID        
INNER JOIN tbLease ON tbUnit.UnitID = tbLease.UnitID

WHERE 
tbAttributeValue.AttributeTemplateDefinitionLinkID in (30, 31, 32)
or tbAttributeValue.AttributeTemplateDefinitionLinkID is null

答案 2 :(得分:0)

当您使用where时,将右表过滤器从ON子句移至Left Outer Join条件,否则Left join将隐式转换为INNER JOIN。试试这个

SELECT DISTINCT tblease.leasetitle, 
                tbattributevalue.attributetemplatedefinitionlinkid, 
                tbattributevalue.value 
FROM   tbbusinessunit 
       LEFT JOIN tbattributevalue 
              ON tbbusinessunit.businessunitid = tbattributevalue.parentid 
                 AND ( tbattributevalue.attributetemplatedefinitionlinkid IN 
                       ( 30, 31, 32 ) 
                        OR tbattributevalue.attributetemplatedefinitionlinkid IS 
                           NULL ) 
       INNER JOIN tbbuildinglinkbusinessunit 
               ON tbbusinessunit.businessunitid = 
                  tbbuildinglinkbusinessunit.businessunitid 
       INNER JOIN tbbuilding 
               ON tbbuildinglinkbusinessunit.buildingid = tbbuilding.buildingid 
       INNER JOIN tbunitlocation 
               ON tbbuilding.buildingid = tbunitlocation.locationid 
       INNER JOIN tbunit 
               ON tbunitlocation.unitid = tbunit.unitid 
       INNER JOIN tblease 
               ON tbunit.unitid = tblease.unitid