代码之家  ›  专栏  ›  技术社区  ›  DCookie

何时或为什么要使用右外部联接而不是左外部联接?

  •  45
  • DCookie  · 技术社区  · 17 年前

    Wikipedia 国家:

    实际上,很少使用显式的右外部联接,因为它们总是可以被左外部联接替换,并且不提供额外的功能

    我想不出使用它的理由。对我来说,这不会让事情变得更清楚。

    编辑: 我是一名甲骨文老手,在新年决心戒掉(+)语法。我想把它做好

    11 回复  |  直到 11 年前
        1
  •  44
  •   Yes - that Jake.    17 年前

    您可能希望对在一对多关系的依赖(多)端具有空行的查询使用左连接,而对在独立端生成空行的查询使用右连接。

    这也可能发生在生成的代码中,或者如果车间的编码要求在FROM子句中指定了表的声明顺序。

        2
  •  23
  •   Michael Buen    17 年前

    B右连接A与左连接B相同

    B RIGHT JOIN A读取:B位于右侧,然后连接A。表示A位于数据集的左侧。与左连接B相同

    如果将左连接重新排列为右连接,则无法获得性能。

    我能想到的使用右连接的唯一原因是,如果你是那种喜欢从内到外思考的人(从细节右连接标题中选择*)。就像其他人一样,比如小端点,其他人喜欢大端点,其他人喜欢自上而下的设计,其他人喜欢自下而上的设计。

    另一种方法是,如果您已经有一个庞大的查询,您想在其中添加另一个表,而重新安排查询是一件非常麻烦的事情,那么只需使用RIGHT JOIN将该表插入现有查询即可。

        3
  •  23
  •   roman    11 年前

    我从来没用过 right join

    enter image description here

    想要得到这样的结果:

    enter image description here

    或者,在SQL(MS SQL Server)中:

    declare @temp_a table (id int)
    declare @temp_b table (id int)
    declare @temp_c table (id int)
    declare @temp_d table (id int)
    
    insert into @temp_a
    select 1 union all
    select 2 union all
    select 3 union all
    select 4
    
    insert into @temp_b
    select 2 union all
    select 3 union all
    select 5
    
    insert into @temp_c
    select 1 union all
    select 2 union all
    select 4
    
    insert into @temp_d
    select id from @temp_a
    union
    select id from @temp_b
    union
    select id from @temp_c
    
    select *
    from @temp_a as a
        inner join @temp_b as b on b.id = a.id
        inner join @temp_c as c on c.id = a.id
        right outer join @temp_d as d on d.id = a.id
    
    id          id          id          id
    ----------- ----------- ----------- -----------
    NULL        NULL        NULL        1
    2           2           2           2
    NULL        NULL        NULL        3
    NULL        NULL        NULL        4
    NULL        NULL        NULL        5
    

    所以如果你换成 left join

    select *
    from @temp_d as d
        left outer join @temp_a as a on a.id = d.id
        left outer join @temp_b as b on b.id = d.id
        left outer join @temp_c as c on c.id = d.id
    
    id          id          id          id
    ----------- ----------- ----------- -----------
    1           1           NULL        1
    2           2           2           2
    3           3           3           NULL
    4           4           NULL        4
    5           NULL        5           NULL
    

    在没有正确联接的情况下执行此操作的唯一方法是使用公共表表达式或子查询

    select *
    from @temp_d as d
        left outer join (
            select *
            from @temp_a as a
                inner join @temp_b as b on b.id = a.id
                inner join @temp_c as c on c.id = a.id
        ) as q on ...
    
        4
  •  10
  •   Bill the Lizard    17 年前

    我唯一一次想到右外部联接是在修复完全联接时,碰巧我需要结果包含右侧表中的所有记录。尽管我很懒,但我可能会非常恼火,以至于我会重新安排它使用左连接。

    Wikipedia 说明了我的意思:

    SELECT *  
    FROM   employee 
       FULL OUTER JOIN department 
          ON employee.DepartmentID = department.DepartmentID
    

    如果你只是替换这个词 FULL 具有 RIGHT 您有了一个新的查询,而不必交换查询的顺序 ON 条款

        5
  •  3
  •   Andrew G. Johnson    17 年前
    SELECT * FROM table1 [BLANK] OUTER JOIN table2 ON table1.col = table2.col
    

    将[空白]替换为:

    右-如果您想要表2中的所有记录,即使它们没有与表1匹配的列(还包括具有匹配项的表1记录)

    大家在说什么?他们是一样的吗?我不这么认为。

        6
  •  3
  •   Matt    13 年前
    SELECT * FROM table_a
    INNER JOIN table_b ON ....
    RIGHT JOIN table_c ON ....
    

        7
  •  3
  •   mattpm    9 年前

    对于正确的连接,我并没有考虑太多,但我想,在编写SQL查询的近20年中,我还没有找到使用正确连接的合理理由。我想我肯定见过很多这样的例子,它们都是由开发人员使用内置查询构建器而产生的。

        8
  •  2
  •   dkretz    17 年前

    但是一个总是可以转换成另一个,优化器在处理一个和另一个时会做得一样好。

    很长一段时间以来,至少有一种主要的rdbms产品只支持左外连接。(我相信是MySQL。)

        9
  •  2
  •   HLGEM    17 年前

    我使用右连接的唯一一次是当我想查看两组数据时,并且我已经从以前编写的查询中获得了左连接或内部连接的特定顺序的连接。在这种情况下,假设您希望将不包含在表a中但包含在表b中的记录视为一组数据,并将不包含在表b中但包含在表a中的记录视为另一组数据。即使这样,我也倾向于这样做,以节省做研究的时间,但如果是要多次运行的代码,我会改变它。

        10
  •  1
  •   Jiri Tousek    9 年前

    FROM 条款-例如。 /*+ORDERED */

    在这种情况下,表在 从…起 RIGHT JOIN 可能有用。

        11
  •  0
  •   Hong Van Vit    9 年前

    with a as(
         select 1 id, 'a' name from dual union all
         select 2 id, 'b' name from dual union all
         select 3 id, 'c' name from dual union all
         select 4 id, 'd' name from dual union all
         select 5 id, 'e' name from dual union all
         select 6 id, 'f' name from dual 
    ), bx as(
       select 1 id, 'fa' f from dual union all
       select 3 id, 'fb' f from dual union all
       select 6 id, 'f' f from dual union all
       select 6 id, 'fc' f from dual 
    )
    select a.*, b.f, x.f
    from a left join bx b on a.id = b.id
    right join bx x on a.id = x.id
    order by a.id
    
    推荐文章