SQL——删除重复对
我正在使用 SQLite 来存储一组使用两列 u 和 v 的图形无向边。例如:
uv
1 2
3 2
2 1
3 4
我已经通过它SELECT DISTINCT * FROM 边缘并删除所有重复的行。
然而,如果我们记住这些是无向边,仍然存在重复项。在上面的示例中,边 (1,2) 出现了两次,一次为 (1,2),一次为 (2,1),两者都是等价的。
我希望删除所有此类重复项,只留下其中一个,(1,2)或(2,1)——哪一个并不重要。
有什么想法如何实现这一目标?谢谢!
I'm using an SQLite to store a set of undirected edges of a graph using two columns, u and v. For example:
u v
1 2
3 2
2 1
3 4
I have already been through it with SELECT DISTINCT * FROM edges and removed all duplicate rows.
However, there are still duplicates if we remember these are undirected edges. In the above example, the edge (1,2) appears twice, once as (1,2) and once as (2,1) which are both equivalent.
I wish to remove all such duplicates leaving only one of them, either (1,2) or (2,1) -- it doesn't really matter which.
Any ideas how to achieve this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果存在相同对(相反),则取 u>v 的对。
If the same pair (reversed) exists take the one where u>v.
这将找到所有重复项:
这将删除重复项:
请注意,这不会删除像 (2, 2) 这样的重复项,但我认为您已经使用 SELECT DISTINCT 获得了这些重复项。
This will find all the duplicates:
This will delete the duplicates:
Note that this will not delete duplicates like (2, 2) but I think you got those already with SELECT DISTINCT.
测试 9 个数字,因此我将 9 个数字添加到两个表中:
然后耦合唯一值而不进行任何重复:
Testing for 9 numbers so I'm adding 9 numbers to two tables:
Then coupling uniques without any repetition: