如果ColA匹配且ColB有时匹配,则删除所有记录的SQL
我需要从一个SQL表中删除所有记录,其中ColumnA与一个ID相匹配,该ID有时ColumnB等于0。换句话说,如果某个ID的任何数据都是错误的,那么请删除所有数据如果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
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)
请用你正在使用的数据库标记你的问题。我想得太多了。非常感谢。