何时或为何使用右外连接而不是左外连接?

发布于 2024-07-10 20:59:40 字数 286 浏览 9 评论 0原文

Wikipedia 指出:

“实际上,很少使用显式右外连接,因为它们始终可以用左外连接替换,并且不提供任何附加功能。”

任何人都可以提供他们更喜欢使用正确符号的情况,为什么? 我想不出使用它的理由。 对我来说,这永远不会让事情变得更清楚。

编辑: 我是一位 Oracle 资深人士,我制定了新年决心,让自己摆脱 (+) 语法。 我想做对

Wikipedia states:

"In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins and provide no additional functionality."

Can anyone provide a situation where they have preferred to use the RIGHT notation, and why?
I can't think of a reason to ever use it. To me, it wouldn't ever make things more clear.

Edit:
I'm an Oracle veteran making the New Year's Resolution to wean myself from the (+) syntax. I want to do it right

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

瞄了个咪的 2024-07-17 20:59:40

我能想到使用 RIGHT OUTER JOIN 的唯一原因是尝试使 SQL 更加自记录。

您可能希望对一对多关系的从属(多)侧具有空行的查询使用左联接,对在独立侧生成空行的查询使用右联接。

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

The only reason I can think of to use RIGHT OUTER JOIN is to try to make your SQL more self-documenting.

You might possibly want to use left joins for queries that have null rows in the dependent (many) side of one-to-many relationships and right joins on those queries that generate null rows in the independent side.

This can also occur in generated code or if a shop's coding requirements specify the order of declaration of tables in the FROM clause.

玩套路吗 2024-07-17 20:59:40

我以前从未使用过右连接,也从未想过我真的需要它,而且它看起来有点不自然。 但经过我的思考,它在这种情况下可能非常有用,当您需要将一个表与多个表的交集进行外部联接时,因此您有这样的表:

在此处输入图像描述

想要得到这样的结果:

在此处输入图像描述

或者,在 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

因此,如果切换到左连接,结果将不一样。

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 ...

I've never used right join before and never thought I could actually need it, and it seems a bit unnatural. But after I thought about it, it could be really useful in the situation, when you need to outer join one table with intersection of many tables, so you have tables like this:

enter image description here

And want to get result like this:

enter image description here

Or, in 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

So if you switch to the left join, results will not be the same.

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

The only way to do this without the right join is to use common table expression or subquery

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 ...
老子叫无熙 2024-07-17 20:59:40

B RIGHT JOIN A 与 A LEFT JOIN B B RIGHT JOIN A 相同

:B ON RIGHT, THEN JOINS A. 表示 A 位于数据集的左侧。 与 A LEFT JOIN B 相同

如果将 LEFT JOIN 重新排列为 RIGHT,则不会获得任何性能。

我能想到为什么要使用 RIGHT JOIN 的唯一原因是,如果您是喜欢从内到外思考的人(从详细信息右连接标题中选择 *)。 就像其他人喜欢小端,其他人喜欢大端,其他人喜欢自上而下的设计,其他人喜欢自下而上的设计。

另一个是,如果您已经有一个巨大的查询,并且想要添加另一个表,那么重新排列查询会很麻烦,因此只需使用 RIGHT JOIN 将表插入现有查询即可。

B RIGHT JOIN A is the same as A LEFT JOIN B

B RIGHT JOIN A reads: B ON RIGHT, THEN JOINS A. means the A is in left side of data set. just the same as A LEFT JOIN B

There are no performance that can be gained if you'll rearrange LEFT JOINs to RIGHT.

The only reasons I can think of why one would use RIGHT JOIN is if you are type of person that like to think from inside side out (select * from detail right join header). It's like others like little-endian, others like big-endian, others like top down design, others like bottom up design.

The other one is if you already have a humongous query where you want to add another table, when it's a pain in the neck to rearrange the query, so just plug the table to existing query using RIGHT JOIN.

赠佳期 2024-07-17 20:59:40

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

Wikipedia 中的这个示例显示了我的意思:

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

如果您只是替换单词 FULLRIGHT 您将获得一个新查询,而无需交换 ON 子句的顺序。

The only time I would think of a right outer join is if I were fixing a full join, and it just so happened that I needed the result to contain all records from the table on the right. Even as lazy as I am, though, I would probably get so annoyed that I would rearrange it to use a left join.

This example from Wikipedia shows what I mean:

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

If you just replace the word FULL with RIGHT you have a new query, without having to swap the order of the ON clause.

相思故 2024-07-17 20:59:40
SELECT * FROM table_a
INNER JOIN table_b ON ....
RIGHT JOIN table_c ON ....

您还能如何快速/轻松地内部联接前 2 个表并与 table_c 联接,同时确保始终选择 table_c 中的所有行?

SELECT * FROM table_a
INNER JOIN table_b ON ....
RIGHT JOIN table_c ON ....

How else could you quickly/easily inner join the first 2 tables and join with table_c while ensuring all rows in table_c are always selected?

深陷 2024-07-17 20:59:40

我确实没有必要对正确的连接考虑太多,但我想在近 20 年的 SQL 查询编写过程中,我还没有找到使用正确连接的合理理由。 我当然见过很多这样的问题,我猜它们是由开发人员使用内置查询生成器产生的。

每当我遇到一个问题时,我都会重写查询以消除它 - 我发现如果您有一段时间没有访问该查询并且它没有,那么它们只是需要太多额外的精神能量来学习或重新学习。查询意图丢失或返回错误结果的情况并不常见 - 通常正是这种不正确性导致要求我检查查询不起作用的原因。

考虑一下,一旦引入右连接,您现在就拥有了我认为需要在中间相遇的竞争逻辑分支。 如果引入额外的要求/条件,这两个分支都可能会进一步扩展,现在您的复杂性会更高,您必须兼顾以确保其中一个分支不会产生不正确的结果。

此外,一旦引入右连接,稍后处理该查询的其他经验不足的开发人员可能会简单地将其他表附加到查询的右连接部分,这样做会扩展仍需要满足的竞争逻辑流。中间; 或者在我见过的某些情况下,开始嵌套视图是因为它们不想触及原始逻辑,也许部分原因是它们可能不理解查询或驱动逻辑的业务规则。

I've not really had to think much on the right join but I suppose that I have not in nearly 20 years of writing SQL queries, come across a sound justification for using one. I've certainly seen plenty of them I'd guess arising from where developers have used built-in query builders.

Whenever I've encountered one, I've rewritten the query to eliminate it - I've found they just require too much additional mental energy to learn or re-learn if you haven't visited the query for some time and it hasn't been uncommon for the intent of the query to become lost or return incorrect results - and it's usually this incorrectness that has led to requests for me to review why the queries weren't working.

In thinking about it, once you introduce a right-join, you now have what I'd consider competing branches of logic which need to meet in the middle. If additional requirements/conditions are introduced, both of these branches may be further extended and you now have more complexity you're having to juggle to ensure that one branch isn't giving rise to incorrect results.

Further, once you introduce a right join, other less-experienced developers that work on the query later may simply bolt on additional tables to the right-join portion of the query and in doing so, expanding competing logic flows that still need to meet in the middle; or in some cases I've seen, start nesting views because they don't want to touch the original logic, perhaps in part, this is because they may not understand the query or the business rules that were in place that drove the logic.

偏闹i 2024-07-17 20:59:40
SELECT * FROM table1 [BLANK] OUTER JOIN table2 ON table1.col = table2.col

将 [BLANK] 替换为:

LEFT - 如果您想要 table1 中的所有记录,即使它们没有与 table2 匹配的列(也包括具有匹配项的 table2 记录)

RIGHT - 如果您想要 table2 中的所有记录,即使它们没有t 有一个与 table1 匹配的列(还包括具有匹配的 table1 记录)

FULL - 如果您想要 table1 和 table2 中的所有记录

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

SELECT * FROM table1 [BLANK] OUTER JOIN table2 ON table1.col = table2.col

Replace [BLANK] with:

LEFT - if you want all records from table1 even if they don't have a col that matches table2's (also included are table2 records with matches)

RIGHT - if you want all records from table2 even if they don't have a col that matches table1's (also included are table1 records with matches)

FULL - if you want all records from table1 and from table2

What is everyone talking about? They're the same? I don't think so.

甜点 2024-07-17 20:59:40

SQL 语句除了正确之外,还应该尽可能易于阅读且表达简洁(因为它们代表单个原子操作,您的大脑需要完全理解它们以避免意外后果。)有时,表达式可以用右外连接。

但一种总是可以转化为另一种,优化器对其中一种的处理效果与另一种一样好。

很长一段时间以来,至少有一种主要的 RDBMS 产品只支持 LEFT OUTER JOIN。 (我相信是 MySQL。)

SQL statements, in addition to being correct, should be as easy to read and expressively concise as possible (because they represent single atomic actions, and your mind needs to grok them completely to avoid unintended consequences.) Sometimes an expression is more clearly stated with a right outer join.

But one can always be transformed into the other, and the optimizer will do as well with one as the other.

For quite a while, at least one of the major rdbms products only supported LEFT OUTER JOIN. (I believe it was MySQL.)

行至春深 2024-07-17 20:59:40

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

The only times I've used a right join have been when I want to look at two sets of data and I already have the joins in a specific order for the left or inner join from a previously written query. In this case, say you want to see as one set of data the records not included in table a but in table b and in a another set the records not in table b but in table a. Even then I tend only to do this to save time doing research but would change it if it was code that would be run more than once.

舂唻埖巳落 2024-07-17 20:59:40

在某些 SQL 数据库中,有优化器提示告诉优化器按照表在 FROM 子句中出现的顺序连接表 - 例如 /*+ORDERED */。 在一些简单的实现中,这甚至可能是唯一可用的执行计划。

在这种情况下,FROM 子句中的表顺序很重要,因此 RIGHT JOIN 可能会很有用。

In some SQL databases, there are optimizer hints that tell the optimizer to join the tables in the order in which they appear in the FROM clause - e.g. /*+ORDERED */ in Oracle. In some simple implementations, this might even be the only execution plan available.

In such cases order of tables in the FROM clause matters so RIGHT JOIN could be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文