我有一个看起来像这样的表:
ID VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7 VAR8 VAR9 VAR10
1 2 1 3 5 2 1 3 5 3 1
2 1 1 1 1 1 1 1 1 1 1
3 1 3 4 7 2 4 1 3 4 6
4 2 2 2 2 2 2 2 2 2 2
5 1 3 5 6 7 4 6 7 4 6
6 3 3 3 3 3 3 3 3 3 3
如何删除具有非不同值的行(在本例中为ID # 2,4,6
),即每列中的相同数字(1,1,1,1,1
)?
答案 0 :(得分:2)
怎么样......
DELETE FROM tablename WHERE
VAR1=VAR2
AND VAR2=VAR3
AND VAR3=VAR4
AND VAR4=VAR5
AND VAR5=VAR6
AND VAR6=VAR7
AND VAR7=VAR8
AND VAR8=VAR9
AND VAR9=VAR10
希望你只需要运行一次 - 我想大多数变量都不会被索引,所以你最终可能会锁定表或删除。最好阻止插入这些行。
答案 1 :(得分:0)
;使用cross apply(values ...)
取消列中的列,然后使用count(distinct ...)=1
进行汇总和过滤:
delete t
where id in (
select t.id
from t
cross apply (values (VAR1),(VAR2),(VAR3),(VAR4),(VAR5),(VAR6),(VAR7),(VAR8),(VAR9),(VAR10)
) u(Var)
group by t.id
having count(distinct u.var)=1
)
rextester演示:http://rextester.com/WZE91559
返回:
+----+------+------+------+------+------+------+------+------+------+-------+
| ID | VAR1 | VAR2 | VAR3 | VAR4 | VAR5 | VAR6 | VAR7 | VAR8 | VAR9 | VAR10 |
+----+------+------+------+------+------+------+------+------+------+-------+
| 1 | 2 | 1 | 3 | 5 | 2 | 1 | 3 | 5 | 3 | 1 |
| 3 | 1 | 3 | 4 | 7 | 2 | 4 | 1 | 3 | 4 | 6 |
| 5 | 1 | 3 | 5 | 6 | 7 | 4 | 6 | 7 | 4 | 6 |
+----+------+------+------+------+------+------+------+------+------+-------+