我需要刪除一個SQL表中的所有記錄,其中ColumnA匹配有時具有ColumnB等於0的ID。換句話說,如果任何ID的數據不好,get擺脫所有數據。SQL刪除所有記錄如果ColA匹配和ColB匹配有時
ColumnA ColumnB
------- -------
11 0
11 1
11 0
12 1
12 1
在這個數據,我想刪除任何地方ColumnA是11,因爲有時ColumnB爲0
我需要刪除一個SQL表中的所有記錄,其中ColumnA匹配有時具有ColumnB等於0的ID。換句話說,如果任何ID的數據不好,get擺脫所有數據。SQL刪除所有記錄如果ColA匹配和ColB匹配有時
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
);
您還可以使用in
:
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)
請將您的問題標記爲您正在使用的數據庫。 –