我有一张 table 看起来像
col1 col2 col3 x y 0.1 y x 0.1 y z 0.2 z y 0.2 .......
(x,y,0.1) 等同于 (y,x,0.1) 因此必须删除其中一个。
基本上表格就像一个矩阵。我需要去掉矩阵对角线上/下方的所有条目。该表有 1 亿个条目 => 结果将有 5000 万个条目。
最佳答案
好吧,如果你知道两个条目都在那里,你可以这样做:
delete from t
where col1 > col2;
如果其中一些可能已经丢失,而您想保留另一个:
delete from t
where col1 > col2 and
exists (select 1
from (select 1
from t t2
where t2.y = t.x and t2.x = t.y
)
)
“double”select
是一种绕过 MySQL 限制的 hack,即您不能在 delete
中使用的子查询中直接引用修改后的表。
编辑:
正如 Ypercube 指出的那样,连接子句可能更好:
delete t
from t join
t t2
on t2.y = t.x and t2.x = t.y and
t.y > t.x;
我实际上发现 in
更容易理解。
关于mysql - 从 SQL 表中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298672/