Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 删除并连接到多个表_Sql Server_Join_Sql Delete - Fatal编程技术网

Sql server 删除并连接到多个表

Sql server 删除并连接到多个表,sql-server,join,sql-delete,Sql Server,Join,Sql Delete,代码: 基本上,我想删除coltype和colsubtype表中未提及coltype和colsubtype的所有记录 我该怎么做呢。下面是我想走的路,但它不起作用——而且——它似乎不是一个好的设计 create table coltype (coltype varchar(5)); insert into coltype values ('typ1'); create table colsubtype (coltype varchar(5), colsubtype varchar(5));

代码:

基本上,我想删除
coltype
colsubtype
表中未提及
coltype
colsubtype
的所有记录

我该怎么做呢。下面是我想走的路,但它不起作用——而且——它似乎不是一个好的设计

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;

您的代码需要大量使用“notexists”操作符

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)
使用不存在:

DELETE FROM table1
WHERE coltype IN 
(SELECT coltype 
 FROM table1 
 WHERE coltype NOT IN (SELECT coltype FROM coltype))
OR colsubtype IN 
(SELECT colsubtype 
 FROM table1 
 WHERE colsubtype NOT IN (SELECT colsubtype FROM colsubtype))
使用左连接:

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)
试试这个

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

坏样本数据?在插入到
colsubtype
表中时,您将“typ2”引用为
coltype
,但您没有将该值插入到
coltype
表中。上述操作很好,只是它还应该删除所有“typ3”也有记录-它没有。刚刚发现/被提醒,来自的第一个
是可选的。呸
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
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
        )