DCo*_*kie 53 sql database right-join
维基百科说:
"在实践中,很少使用明确的右外连接,因为它们总是可以用左外连接替换,并且不提供额外的功能."
任何人都可以提供他们更喜欢使用正确表示法的情况,为什么?我想不出使用它的理由.对我来说,它永远不会让事情更清楚.
编辑:我是甲骨文老将制作新年决议,以便从(+)语法中脱离自己.我想做得对
Yes*_*ke. 35
我能想到使用RIGHT OUTER JOIN的唯一原因是尝试让你的SQL更加自我记录.
你有可能会想使用左联接为有一个一对多的关系的依赖(多)边空行和右连接于那些产生于独立的侧空行查询的查询.
这也可能发生在生成的代码中,或者商店的编码要求指定FROM子句中表的声明顺序.
Rom*_*kar 16
我以前从未使用right join过,从未想过我真的需要它,而且看起来有点不自然.但是在我考虑它之后,它可能在这种情况下非常有用,当你需要将一个表外连接到多个表的交集时,所以你有这样的表:

并希望得到这样的结果:

或者,在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
Run Code Online (Sandbox Code Playgroud)
因此,如果切换到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
Run Code Online (Sandbox Code Playgroud)
没有正确连接的唯一方法是使用公用表表达式或子查询
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 ...
Run Code Online (Sandbox Code Playgroud)
Mic*_*uen 15
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将表插入现有查询.
我认为一个右外的唯一一次参加是,如果我被固定一个完整的加盟,它只是碰巧,我需要的结果包含在右表中的所有记录.即使懒惰的我,虽然,我可能会得到这么生气,我会重新安排它使用左连接.
维基百科的这个例子显示了我的意思:
SELECT *
FROM employee
FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID
Run Code Online (Sandbox Code Playgroud)
如果您只是替换单词FULL,则RIGHT您有一个新查询,而无需交换该ON子句的顺序.
SELECT * FROM table_a
INNER JOIN table_b ON ....
RIGHT JOIN table_c ON ....
Run Code Online (Sandbox Code Playgroud)
您还能如何快速/轻松地内部联接前 2 个表并与 table_c 联接,同时确保始终选择 table_c 中的所有行?
| 归档时间: |
|
| 查看次数: |
31105 次 |
| 最近记录: |