首页 > 解决方案 > 从 SQL 表中删除重复的对

问题描述

我有一个表格示例,如下所述:

样本数据集

该表基本上有 2 个重复项,即海得拉巴和班加罗尔之间的距离与孟买和德里相同。

我想从我的表中删除这些重复项。有没有办法用 SQL 做到这一点?

标签: sql

解决方案


您可以使用not existsand执行此操作or

select t.*
from t
where source < destination or
      (source > destination and
       not exists (select 1
                   from t t2
                   where t2.source = t.destination and
                         t2.destination = t.source and
                         t2.distance = t.distance
                  )
       );

这是标准 SQL,应该适用于任何数据库。


推荐阅读