외부 조인, 오른쪽 외부 조인 및 완전 외부 조인 리턴을 남긴 레코드 수를 찾을 수 있습니까? 왼손잡이 보조 테이블과 우 편 사이드 테이블과 일치하는 레코드의 레코드 수를 감안할 때.외부 조인, 오른쪽 외부 조인 및 완전 외부 조인 리턴을 남긴 레코드 수를 찾으십시오.
나는 그들 사이의 관계를 서로 연결하려고합니다. 나는 샘플 데이터를 입력하는 두 개의 테이블로 시도했다. 두 사람 사이에 어떤 관계도 가져올 수 없습니다. 왼쪽 사이드 테이블에서 일치하지 않는 항목의 수를 알면 어떻게 될까요? 이 숫자를 일치하는 레코드에 추가 한 다음 왼쪽 외부 조인 출력을 가져옵니다. 오른손 쪽에서 필적 할 수없는 레코드의 수를 알고 있으면 그 레코드를 해당 레코드에 추가합니다. 그것은 우리에게 바로 외부 조인 출력을 줄 것입니다.
일치하지 않는 레코드를 알지 못해도 가능합니다. 외부 조인, 오른쪽 외부 조인 및 완전 외부 조인 리턴을 남긴 레코드 수를 찾을 수 있습니까?
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
단일 쿼리 또는 세 가지 다른 쿼리에서 필요합니까? –
나는 그 질문을 이해하지 못한다.서로 다른 조인 유형으로 얻는 레코드 수를 표시하고 일치하는 레코드가없는 레코드를 계산하는 방법도 보여줍니다. 그래서 당신은 아직도 무엇을 찾고 있습니까? –
그 번호를 얻는 유일한 방법은 JOIN 쿼리를 실행하는 것입니다. FULL JOIN을 실행하여 한꺼번에 3 개를 모두 얻으십시오. – Serg