Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete with join to multiple tables

Code:

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;

Basically, I want to delete all records where the coltype and colsubtype is not mentioned in the coltype and colsubtype tables.

How do I do that. The below is path I was thinking of taking but it does not work - and - it does not seem like a good design.

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)
like image 717
Needs Help Avatar asked Jan 10 '11 17:01

Needs Help


People also ask

Can we use delete with join?

We use joins to combine data from multiple tables. To delete the same rows or related rows from the table at that time we use delete join.

Can you delete from two tables at once?

The syntax also supports deleting rows from multiple tables at once. To delete rows from both tables where there are matching id values, name them both after the DELETE keyword: DELETE t1, t2 FROM t1 INNER JOIN t2 ON t1.id = t2.id; What if you want to delete nonmatching rows?

How do you delete multiple tables at a time in SQL?

Alternatively, you can also hit keyboard option F7 and it will open up Object Explorer Details. In Object Explorer Details, select the tables which you want to delete and either hit the keyboard button DELETE or just go right click on the tables and select the option DELETE.


2 Answers

Using NOT EXISTS:

delete from t1 
    from table1 t1
    where not exists (select null from coltype ct where ct.coltype = t1.coltype)
       or not exists (select null from colsubtype cst where cst.colsubtype = t1.colsubtype)

Using LEFT JOINs:

delete from t1 
    from table1 t1
        left join coltype ct
            on t1.coltype = ct.coltype
        left join colsubtype cst
            on t1.colsubtype = cst.colsubtype
    where ct.coltype is null 
       or cst.colsubtype is null
like image 116
Joe Stefanelli Avatar answered Oct 18 '22 07:10

Joe Stefanelli


Give this a try

delete from table1
where not exists
        (
        select *
        from coltype
        where table1.coltype = coltype.coltype
        )
    and not exists
        (
        select *
        from colsubtype
        where table1.coltype = colsubtype.coltype
            and table1.colsubtype = colsubtype.colsubtype
        ) 
like image 2
bobs Avatar answered Oct 18 '22 09:10

bobs