テーブルA ID -- 1 2 3 4 テーブルB ID EntryDate -- ---------- 1 2005-08-06 3 2005-09-06 4 2004-09-06 テーブルAの中で、 同一IDがテーブルBに存在して、かつ、 テーブルBのEntryDateが2005年9月のデータを削除する。
create table テーブルA(
ID number(1),
primary key(ID));
create table テーブルB(
ID number(1),
EntryDate date,
primary key(ID));
insert into テーブルA(ID) values(1);
insert into テーブルA(ID) values(2);
insert into テーブルA(ID) values(3);
insert into テーブルA(ID) values(4);
insert into テーブルB(ID,EntryDate) values(1,to_date('20050806','yyyymmdd'));
insert into テーブルB(ID,EntryDate) values(3,to_date('20050906','yyyymmdd'));
insert into テーブルB(ID,EntryDate) values(4,to_date('20040906','yyyymmdd'));
commit;
delete from テーブルA a
where exists(select 1 from テーブルB b
where b.ID=a.ID
and to_char(b.EntryDate,'yyyymm') = '200509');
delete文でも相関サブクエリが使用できます。