Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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
如果ColA匹配且ColB有时匹配,则删除所有记录的SQL_Sql_Sql Server - Fatal编程技术网

如果ColA匹配且ColB有时匹配,则删除所有记录的SQL

如果ColA匹配且ColB有时匹配,则删除所有记录的SQL,sql,sql-server,Sql,Sql Server,我需要从一个SQL表中删除所有记录,其中ColumnA与一个ID相匹配,该ID有时ColumnB等于0。换句话说,如果某个ID的任何数据都是错误的,那么请删除所有数据 ColumnA ColumnB ------- ------- 11 0 11 1 11 0 12 1 12 1 在此数据中,我想删除ColumnA为11的任何内容,因为有时Co

我需要从一个SQL表中删除所有记录,其中ColumnA与一个ID相匹配,该ID有时ColumnB等于0。换句话说,如果某个ID的任何数据都是错误的,那么请删除所有数据

ColumnA    ColumnB
-------    -------
     11          0
     11          1
     11          0
     12          1
     12          1

在此数据中,我想删除ColumnA为11的任何内容,因为有时ColumnB为0。

ANSI标准语法为:

delete from t
    where exists (select 1
                  from t t2
                  where t2.ColumnA = t.ColumnA and t2.ColumnB = 0
                 );
您也可以在中使用

delete from t
    where t.ColumnA in (select t2.ColumnA
                        from t t2
                        where t2.ColumnB = 0
                       );
简单到:

delete from tablename
where columnA in (select columnA from tablename where columnB = 0)

请用你正在使用的数据库标记你的问题。我想得太多了。非常感谢。