左外连接不返回预期的结果集

Posted

技术标签:

【中文标题】左外连接不返回预期的结果集【英文标题】:Left Outer join not returning expected resultSet 【发布时间】:2017-01-20 11:37:49 【问题描述】:

我有这两张桌子

              #TABLE1#
##CompanyName##    ##PrimaryKey##
DELL                     1
DELL CALIFORNIA          2
DELL SAN FRANCISCO       3
DELL LOS ANGELES         4
IBM                      5
GOOGLE                   6

                                     #TABLE2#
##ParentComp##   ##ParentPrimaryKey##       ##ChildComp##    ##ChildPrimaryKey##
    DELL                     1             DELL CALIFORNIA            2
DELL CALIFORNIA              2             DELL SAN FRANCISCO         3
DELL CALIFORNIA              2             DELL LOS ANGELES           4

现在预期的表格如下所示;关系(层次结构)列只有三个可能的值(父/子/独立),如图所示:

                               #TABLE3#
##CompanyName##             ##Relationship##                     ##ParentCompany##
DELL                             PARENT                                ---
DELL CALIFORNIA                  CHILD                                 DELL
DELL SAN FRANCISCO               CHILD                             DELL CALIFORNIA
DELL LOS ANGELES                 CHILD                             DELL CALIFORNIA
IBM                           INDEPENDENT                              ---
GOOGLE                        INDEPENDENT                              ---

我已经尝试了以下两个加入选项

选择 ... 表1 左外连接 Table2 ON Table1.PrimaryKey = Table2.ParentPrimaryKey 左外连接 Table2 tAlias2 Table2.ParentPrimaryKey = tAlias2.ChildPrimaryKey

返回

 ##CompanyName##               ##Relationship##                     ##ParentCompany##
    DELL                             PARENT                                ---
    DELL CALIFORNIA                  CHILD                                 DELL
    DELL SAN FRANCISCO            INDEPENDENT                              ---
    DELL LOS ANGELES              INDEPENDENT                              ---
    IBM                           INDEPENDENT                              ---
    GOOGLE                        INDEPENDENT                              ---

选择 ... 表1 左外连接 Table2 ON Table1.PrimaryKey = Table2.ChildPrimaryKey 左外连接 Table1 tAlias1 Table2.ParentPrimaryKey = tAlias1.PrimaryKey

将返回如下内容:

 ##CompanyName##               ##Relationship##                     ##ParentCompany##
    DELL                           INDEPENDENT                             ---
    DELL CALIFORNIA                  CHILD                                 DELL
    DELL SAN FRANCISCO               CHILD                              DELL CALIFORNIA
    DELL LOS ANGELES                 CHILD                              DELL CALIFORNIA
    IBM                            INDEPENDENT                             ---
    GOOGLE                         INDEPENDENT                             ---

【问题讨论】:

mysql 和 ms sql 是两个不同的产品。请删除不相关的标签。 可能类似于SELECT T.companyName, MAX(COALESCE(T2.relationship, T2A.relationship, 'INDEPENDENT')) relationship, MAX(T2.parentCompany) parentCompany FROM table1 T LEFT JOIN (SELECT 'CHILD', T2.childPrimaryKey, T1.companyName FROM table2 T2 JOIN table1 T1 ON T1.primaryKey = T2.parentPrimaryKey) T2(relationship, childPrimaryKey, parentCompany) ON T2.childPrimaryKey = T.primaryKey LEFT JOIN (SELECT 'PARENT', parentPrimaryKey FROM table2) T2A(relationship, parentPrimaryKey) ON T2A.parentPrimaryKey = T.primaryKey GROUP BY T.companyName; 【参考方案1】:

当您加入talias2.ChildPrimaryKey 时,您需要加入Table1.PrimaryKey 列。

SELECT ..
FROM   table1 
       LEFT OUTER JOIN table2 
                    ON table1.primarykey = table2.parentprimarykey 
       LEFT OUTER JOIN table2 talias2 
                    ON table1.primarykey = talias2.childprimarykey --here

当您将tAlias2.ChildPrimaryKeytable2.parentprimarykey 联接时,只有来自table2table1(table1.primarykey = table2.parentprimarykey) 的匹配记录将与tAlias2.ChildPrimaryKey 联接

【讨论】:

【参考方案2】:

您的架构显然是多余的。您可以删除table2 并从关注中获得您想要的一切。

declare @tbl table (CompanyName varchar(100),PrimaryKey int primary key, parentId int)

insert @tbl values
('DELL',                     1,null),
('DELL CALIFORNIA',          2,1),
('DELL SAN FRANCISCO',       3,2),
('DELL LOS ANGELES',         4,2),
('IBM',                      5,null),
('google',                   6,null)


;with tbl as (
select PrimaryKey, CompanyName, parentId,
rel = cast(case when exists(select 1 from @tbl t1 where t1.parentId=t.PrimaryKey) then 'parent'
      else 'independent' end as varchar(100)),
cast(null as varchar(100)) parentName
from @tbl t
where t.parentId is null
union all
select t.PrimaryKey, t.CompanyName, t.parentId,cast('child' as varchar(100)),
tbl.CompanyName
from @tbl t inner join tbl on t.parentId=tbl.PrimaryKey
)
select * from tbl

order by PrimaryKey

【讨论】:

以上是关于左外连接不返回预期的结果集的主要内容,如果未能解决你的问题,请参考以下文章

左外连接没有给出任何结果

添加 2 表条件时,Oracle 左外连接停止返回结果

左外连接和右外连接的区别

左外连接不返回主表中的所有记录

左外连接右外连接内连接全连接的概念

Spark 使用左外连接给出不正确的结果