sql - 从 SQL 表中删除重复的对
问题描述
我有一个表格示例,如下所述:
该表基本上有 2 个重复项,即海得拉巴和班加罗尔之间的距离与孟买和德里相同。
我想从我的表中删除这些重复项。有没有办法用 SQL 做到这一点?
解决方案
您可以使用not exists
and执行此操作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,应该适用于任何数据库。
推荐阅读
- ios - 操作表在 ipad ios 13.6 中不起作用
- c++ - QT - 使用 QSerialPort 写入和读取(ASCII 命令)
- objective-c - 如何从 NSTextField 设置静态 int
- reactjs - 提交反应后清除状态
- sql - 如何在 Oracle SQL 19 中根据组填充所有行的空值?
- java - 如何在测试 Spring Boot 应用程序的 API 时在 RequestBody 中传递对象
- algorithm - 节点 s 的子树中值为 x 的节点数?
- javascript - 在 React Native 中使用 formData 调用 API
- tensorflow - pytorch 关于 3D 输入的梯度
- html - 可搜索的下拉列表 html