代碼:刪除與加入到多個表
create table coltype (coltype varchar(5));
insert into coltype values ('typ1');
create table colsubtype (coltype varchar(5), colsubtype varchar(5));
insert into colsubtype values ('typ2', 'st1');
insert into colsubtype values ('typ2', 'st2');
create table table1 (col1 varchar(5), coltype varchar(5), colsubtype varchar(5));
insert into table1 values ('val1','typ1', 'st1');
insert into table1 values ('val2','typ1', 'st2');
insert into table1 values ('val3','typ1', 'st3');
insert into table1 values ('val4','typ2', 'st1');
insert into table1 values ('val5','typ2', 'st2');
insert into table1 values ('val6','typ2', 'st3');
insert into table1 values ('val7','typ3', 'st1');
insert into table1 values ('val8','typ3', 'st2');
insert into table1 values ('val9','typ3', 'st3');
commit;
基本上,我想刪除其中coltype
和colsubtype
未在coltype
和colsubtype
表中提到的所有記錄。
我該怎麼做。以下是我正在考慮的路徑,但它不起作用 - 並且 - 它似乎不是一個好的設計。
delete from table1
where coltype != (select coltype from coltype)
OR not (coltype = cst.coltype and colsubtype = cst.colsubtype
from (select coltype, colsubtype from colsubtype) cst)
樣品不良?您在'colsubtype`表的插入中將'typ2'引用爲`coltype`,但是您沒有將該值插入到`coltype`表中。 – 2011-01-10 17:34:05