oracle 一对一左外连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1703418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:24:16  来源:igfitidea点击:

ONE TO ONE LEFT OUTER JOIN

sqloracle

提问by Sheldon

I was wondering, is there a way to make a kind of one to one left outer join:

我想知道,有没有办法进行一种一对一的左外连接:

I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, but there exists only ONE record that match that condition, so when it found its pair on B, it must stop and continue with the next row at table A.

我需要一个连接来匹配表 A 和表 B,对于表 A 上的每条记录,它必须在表 B 上搜索它的对,但是只有一条记录匹配该条件,所以当它在 B 上找到它的对时,它必须停止并继续表 A 的下一行。

What I have is a simple LEFT OUTER JOIN.

我拥有的是一个简单的左外连接。

select *  from A LEFT OUTER JOIN B ON A.ID = B.ID ORDER BY (NAME) ASC

Thanks in advance!

提前致谢!

采纳答案by Adam Robinson

The syntax you present in your question is correct. There is no difference in the query for joining on a one-to-one relationship than on a one-to-many.

您在问题中提出的语法是正确的。加入一对一关系的查询与加入一对多关系的查询没有区别。

回答by HLGEM

SQL doesn't work this way. In the first place it does not look at things row-by-row. In the second place what defines the record you want to match on?

SQL 不是这样工作的。首先,它不会逐行查看事物。其次,什么定义了您要匹配的记录?

Assuming you don't really care which row is selcted, something like this might work:

假设您并不真正关心选择了哪一行,这样的事情可能会起作用:

SELECT * 
From tableA
left outer join 
(select b.* from tableb b1
join (Select min(Id) from tableb group by id) b2 on b1.id - b2.id) b
on a.id = b.id

BUt it still is pretty iffy that you wil get the records you want when there are multiple records with the id in table b.

但是,当表 b 中有多个带有 id 的记录时,您是否会获得所需的记录仍然很不确定。