Code:Löschen verbinden mit, um mehrere Tabellen
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;
Grundsätzlich möchte ich alle Datensätze löschen, wo die coltype
und colsubtype
nicht in den coltype
und colsubtype
Tabellen erwähnt.
Wie mache ich das? Das Unten ist der Weg, den ich mir vorgestellt habe, aber es funktioniert nicht - und - es scheint kein gutes Design zu sein.
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)
Falsche Beispieldaten? Sie referenzieren "typ2" als "coltype" in den Einfügungen in die "colsubtitype" -Tabelle, aber Sie haben diesen Wert nicht in die "coltype" -Tabelle eingefügt. –