2013-01-08 2 views
0

예 :두 테이블을 비교하여 누락 된 행을 추출합니다.

단순화 된 사례 테이블 :

SQL Table A which contain the missing row entry in table B

select count(1) from table_a; --> returns 5 results 
select count(1) from table_b; --> returns 4 results 
select count(1) from table_a, table_b2 b 
where b.id_ab like a.id_ab; --> returns 4 results 
select count(1) from table_a, table_b2 b 
where b.id_ab not like a.id_ab; --> returns unexpected result 

:

이 (제외) 만 발생하는 오류를 시도했다.

select a.id_ab from table_a a, table_b b except select a.id_ab from table_a, table_b2 b 
where b.id_ab not like a.id_ab; 

또는 노조를 사용하는 방법은 무엇입니까? 예 :

(Select * from table_a except select * from table_b) Union All (Select * from table_b  except select record_id from table_a); 

예상 결과 :

results

감사합니다.

답변

2
select a.id_ab 
from table_a a 
    LEFT JOIN table_b b on a.id_ab = b.id_ab 
where b.id_ab is null 
+0

감사를 좋아한다. 완벽하게 작동합니다. – user1872384

1

아래의 쿼리를 사용하십시오 (왼쪽 외부 조인 추가).

table_a와 일치하지 않는 데이터를 가져옵니다.

select count(1) from table_a, table_b2 b 
where b.id_ab(+) = a.id_ab; 

당신이 같아야 쿼리의 열 사이 UNION을 사용하는 경우 유는 쿼리

select count(1) from table_a, table_b2 b 
where b.id_ab <> a.id_ab; 

아래 행 사용 누락합니다.

SELECT col1,col2 from table_a 
    UNION 
    SELECT col1,col2 from table_b 

하지만이 legendofawesomeness

SELECT col1,col2 from table_a 
    UNION 
    SELECT col1 from table_b 
+0

노조 비루에 대한 조언 주셔서 감사합니다. – user1872384

관련 문제