2016-07-13 4 views
0

MYSQL에 table2에 ID 범위가 있고 table1에 id 값이 포함 된 두 개의 테이블이 있습니다. table2 범위에없는 table1 ID를 구분하고 싶습니다. 이 쿼리 :MYSQL - 모든 범위가 아님

select id 
from table1,table2 
where table1.id not between table2.start and table2.end 

적어도 하나의 범위 사이에없는 ID가 반환됩니다. 하지만 모든 범위에 속하지 않는 ID를 얻고 싶습니다.

어떤 아이디어가 있습니까?

답변

0

사용할 수

(는 많은 자원이 필요하기 때문에 나는 안티 조인을 사용할 그나마) not exists :

select t1.* 
from table1 t1 
where not exists (select 1 
        from table2 t2 
        where t1.id between t2.start and t2.end 
       ); 
0

당신이 원한다면 내가 바로 그것을 가지고 있다면 모르겠지만 table2의 ID 범위를 벗어나는 table1의 ID를 가져 오려면 다음을 시도하십시오.

select id from table1 
       where table1.id not between 
       (select min(table2.id) from table2) and 
        (select max(table2.id) from table2)