首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL:在有向图中获取无向链接

SQL:在有向图中获取无向链接
EN

Stack Overflow用户
提问于 2014-08-22 13:19:12
回答 1查看 70关注 0票数 0

我在有向图中有一张保持边的表格:

代码语言:javascript
复制
CREATE TABLE edges ( 
    from_here int not null, 
    to_there  int not null
)

如何在此表上进行选择而不考虑所有反向链接?例如,从本表中:

代码语言:javascript
复制
+------------+----------+--+
| from_there | to_there |  |
+------------+----------+--+
|          1 |        2 |  |
|          2 |        1 |  |
|          3 |        4 |  |
+------------+----------+--+

我想得到这个结果:

代码语言:javascript
复制
+------------+----------+--+
| from_there | to_there |  |
+------------+----------+--+
|          1 |        2 |  |
|          3 |        4 |  |
+------------+----------+--+

换句话说,如何才能得到每个双向链路--仅仅是前向链路?我们不能假定互惠边总是存在的。

编辑目标是每个双向链接获得一行,不管是前向还是后向链路。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-22 13:31:36

如果我做对了

代码语言:javascript
复制
select from_there, to_there
from edges x
where not exists (
    select 1 from edges y 
    where x.from_there = y.to_there
      and x.to_there = y.from_there
      and x.to_there < y.to_there
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25448033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档