从SQL表中删除重复的对

问题描述 投票:-1回答:1

下面有我的表格示例:

“样本数据集”

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

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

sql
1个回答
0
投票

您可以使用not existsor

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

这是标准的SQL,应该可以在任何数据库中使用。

© www.soinside.com 2019 - 2024. All rights reserved.