找到离开外连接,右外连接和全外连接返回的记录数

时间:2016-09-18 17:38:02

标签: sql oracle

是否可以找到左外连接,右外连接和全外连接返回的记录数。给出左手边桌和右手边桌的记录数和匹配记录。

我正在尝试关联它们之间的关系。我尝试过两个表输入样本数据。无法在他们之间获得任何关系。 如果我知道左手边桌中不匹配的条目数量。我将该数字添加到匹配的记录中,然后我将获得左外连接输出。如果我知道右手边的不匹配记录数,那么我会将该数字添加到匹配的记录中。它将为我们提供正确的外连接输出。

在不知道无与伦比的记录的情况下是否可行。我们能否找到留下外连接,右外连接和全外连接返回的记录数。

CREATE table table1(
    id integer,
    name varchar(40)        
);

CREATE table table2(
        id integer,
    name varchar(40)        
);

insert into table1(id,name)values(1,'ABC');
insert into table1(id,name)values(2,'DEF');
insert into table1(id,name)values(3,'GHI');
insert into table1(id,name)values(4,'JKL');
insert into table1(id,name)values(5,'JKL');
insert into table1(id,name)values(6,'JKL');

insert into table2(id,name)values(2,'ABC');
insert into table2(id,name)values(2,'ABC');
insert into table2(id,name)values(1,'ABC');
insert into table2(id,name)values(1,'ABC');
insert into table2(id,name)values(3,'ABC');
insert into table2(id,name)values(3,'ABC');
insert into table2(id,name)values(4,'ABC');
insert into table2(id,name)values(4,'ABC');
insert into table2(id,name)values(5,'ABC');
insert into table2(id,name)values(5,'ABC');

insert into table2(id,name)values(11,'ABC');
insert into table2(id,name)values(12,'ABC');
insert into table2(id,name)values(13,'ABC');
insert into table2(id,name)values(14,'ABC');
select count(*) from table1;//6
select count(*) from table2; //14

select count(*) from table1 inner join table2
on table1.id=table2.id;   //10

select count(*) from table1 left outer join table2
on table1.id=table2.id;//11


select count(*) from table1 right outer join table2
on table1.id=table2.id;//14

select count(*) from table1 full outer join table2
on table1.id=table2.id;//15


//Unmatched records`enter code here`
select count(*) from table1 left outer join table2
on table1.id=table2.id
where table2.id is null;//1

select count(*) from table1 right outer join table2
on table1.id=table2.id
where table1.id is null;//4

2 个答案:

答案 0 :(得分:0)

Table2中的id不是唯一的,但table1中的id是。
所以我猜猜table2.id是table1.id的外键。

在这种情况下,这个带有FULL JOIN的SQL可以组合匹配和不匹配的计数:

select 
count(distinct case when t2.id is null then t1.id end) as total_unmatched_t1,
count(case when t1.id is null then t2.id end) as total_unmatched_t2,
count(distinct case when t2.id is not null then t1.id end) as total_matched_t1,
count(case when t1.id is not null then t2.id end) as total_matched_t2
from table1 t1
full outer join table2 t2 on (t1.id = t2.id);

答案 1 :(得分:0)

由于您知道记录总数,因此知道不匹配记录的数量相当于知道匹配记录的数量。 (也许!)

您的问题的答案是否定的,您只能通过了解基表的基数来确定不同类型的连接中的记录数,而不知道有多少记录匹配(或不匹配)。简单的心理锻炼:两个表都有100条记录。如果所有连接完全匹配,则一对一,则所有连接与内连接相同,并且它们都有100条记录。如果根本没有匹配项,则内部联接为零行,单向联接为100行,完整外部联接为200行。这些案例之间的唯一区别是匹配(或不匹配)记录的数量,没有其他任何你可能知道的,这将使你得到没有这条信息的答案。

在OP询问后续问题后增加了

实际上知道"匹配了多少条记录"没有明确的定义,也不够。假设两个表中的所有记录都匹配。在一个极端,匹配可以是成对的:两个表中都有一个id列,两个表中的值都是从1到100的所有可能值。然后结果有100行。另一方面,假设" id"在两个表中都不是唯一的。相反,它的值为1 IN ALL 100 ROWS IN BOTH TABLES。然后第一个表中的每一行都匹配第二个表中的每一行,结果集将有100 x 100 = 10,000行。

这只是建议如下:"匹配了多少行"不是一个定义明确的概念。要计算结果连接(不同类型)的计数,需要知道连接的内容,并且对于连接条件中的每个元组,每个表中具有该特定元组的行数。然后,内连接的结果集中的行数是这种元组分组计数的乘积之和,以及来自外连接的左(或右或两)表的不匹配行的附加行。 / p>