SQL——删除重复对

发布于 2024-10-11 22:46:32 字数 337 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的海 2024-10-18 22:46:32

如果存在相同对(相反),则取 u>v 的对。

SELECT DISTINCT u,v
FROM table t1 
WHERE t1.u > t1.v
    OR NOT EXISTS (
        SELECT * FROM table t2 
            WHERE t2.u = t1.v AND t2.v = t1.u 
    )

If the same pair (reversed) exists take the one where u>v.

SELECT DISTINCT u,v
FROM table t1 
WHERE t1.u > t1.v
    OR NOT EXISTS (
        SELECT * FROM table t2 
            WHERE t2.u = t1.v AND t2.v = t1.u 
    )
守护在此方 2024-10-18 22:46:32

这将找到所有重复项:

SELECT t1.u, t1.v FROM table t1 INNER JOIN table t2
 ON t1.u = t2.v AND t1.v = t2.u

这将删除重复项:

DELETE FROM table t1 WHERE
  EXISTS (SELECT * FROM table t2 WHERE t2.u = t1.v AND t2.v = t1.u AND t1.u > t2.u)

请注意,这不会删除像 (2, 2) 这样的重复项,但我认为您已经使用 SELECT DISTINCT 获得了这些重复项。

This will find all the duplicates:

SELECT t1.u, t1.v FROM table t1 INNER JOIN table t2
 ON t1.u = t2.v AND t1.v = t2.u

This will delete the duplicates:

DELETE FROM table t1 WHERE
  EXISTS (SELECT * FROM table t2 WHERE t2.u = t1.v AND t2.v = t1.u AND t1.u > t2.u)

Note that this will not delete duplicates like (2, 2) but I think you got those already with SELECT DISTINCT.

忘东忘西忘不掉你 2024-10-18 22:46:32

测试 9 个数字,因此我将 9 个数字添加到两个表中:

 declare @num  int
 set @num =1
 while @num<10
 begin 
 insert into t2 values (@num)
 insert into t1 values (@num)
 set @num +=  1 
 end

然后耦合唯一值而不进行任何重复:

 select t1.u, t2.v
 from t1 cross join t2
 where t1.u>t2.v

Testing for 9 numbers so I'm adding 9 numbers to two tables:

 declare @num  int
 set @num =1
 while @num<10
 begin 
 insert into t2 values (@num)
 insert into t1 values (@num)
 set @num +=  1 
 end

Then coupling uniques without any repetition:

 select t1.u, t2.v
 from t1 cross join t2
 where t1.u>t2.v
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文