我有一张看起来像的桌子
col1 col2 col3 xy 0.1 yx 0.1 yz 0.2 zy 0.2 …………
(x,y,0.1) 等价于 (y,x,0.1) 因此必须删除其中之一。
基本上,表格就像一个矩阵。我需要摆脱矩阵对角线上方/下方的所有条目。该表有 1 亿个条目 => 结果将有 5 千万个条目。
好吧,如果您知道两个条目都在那里,您可以执行以下操作:
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 中的限制,即您不能在delete.
编辑:
正如 Ypercube 指出的那样,join 子句可能更好:
delete t
    from t join
         t t2
         on t2.y = t.x and t2.x = t.y and
            t.y > t.x;
我实际上发现in更容易理解。
尝试多表DELETE。
语法并不容易。类似的东西(假设你的表被命名tbl):
DELETE tbl FROM tbl, tbl AS t2
    WHERE tbl.col1 = t2.col2 
        AND tbl.col2 = t2.col1 
        AND tbl.col3 = t2.col3
        AND tbl.col1 > tbl.col2
    Sylvain 的解决方案应该有效。这是使用 SubQ 的替代方法。
delete from mytable where (col1,col2)in(sel col2,col1 from mytable where col1>col2);