2014-11-09 2 views
1

table1과 table2 사이에 1 N 개의 관계가 있습니다. table2의 "code"열은 table1의 외래 키입니다. table2에 관련된 행이없는 table1의 모든 행을 삭제하려고합니다. 어쩌면 이것은 당신이 원하는 것입니다 ...두 테이블의 행을 삭제하십시오.

답변

1

을 나는 시도

delete * from table1 r 
    inner join table2 a 
     where (r.code!=a.code) 

하지만이 두 테이블의 모든 행을 삭제 하시겠습니까?

delete from table1 
where code not in (
    select code from table2 
) 

당신이 실행하기 전에 올바른 행을 선택 쿼리 사용하여 삭제됩니다 확인 할 수 있습니다 삭제 :

select * from table1 
where code not in (
    select code from table2 
) 
1

당신이 시도 할 수 있습니다 :

delete from table1 r 
where not exists (select 1 from table2 a where r.code = a.code); 
+0

@jpw 고마워요. 질문의 진술을 복사했는데 – Multisync

0

또는 아마를 를 사용하여 RIGHT OUTER JOIN

delete from table1 r 
    right join table2 a on a.code = r.code 
     where r.code is null 
관련 문제