SQL:删除列之间的重复行

时间:2014-06-03 17:31:27

标签: sql sqlite duplicates

我有一个以下列格式加载到sqlite中的表:

player1 player2 player1Probability player2Probability
John    Peter   0.6                0.4
Peter   John    0.4                0.6
Mary    Bob     0.8                0.2
Bob     Mary    0.2                0.8

如您所见,不同列中存在重复项。前两行在它们提供的数据中基本相同:我们可以看到约翰有0.6概率,彼得从第一行有0.4,而第二行只是重复这些信息。

有没有办法通过SQL查询删除重复的行?

1 个答案:

答案 0 :(得分:-1)

这就是我在MySQL中的做法,我希望在sqlite中可以这样做。

DELETE t1.*
FROM YourTable AS t1
JOIN YourTable AS t2 
ON t1.player1 = t2.player2 
    AND t1.player2 = t2.player1 
    AND t1.player1 > t1.player2

另一种方式:

DELETE FROM YourTable AS t1
WHERE EXISTS (SELECT *
              FROM YourTable AS t2
              WHERE t1.player1 = t2.player2
                 AND t1.player2 = t1.player1
                 AND t2.player1 < t1.player2)