SQL删除所有记录如果ColA匹配和ColB匹配有时

时间:2016-11-21 15:01:51

标签: sql sql-server

我需要从SQL表中删除所有记录,其中ColumnA匹配有时将ColumnB等于0的ID。换句话说,如果ID的任何数据都不好,请删除所有数据。

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

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

2 个答案:

答案 0 :(得分:1)

ANSI标准语法是:

delete from t
    where exists (select 1
                  from t t2
                  where t2.ColumnA = t.ColumnA and t2.ColumnB = 0
                 );

您还可以使用in

delete from t
    where t.ColumnA in (select t2.ColumnA
                        from t t2
                        where t2.ColumnB = 0
                       );

答案 1 :(得分:0)

一样简单:

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