在SQL中的有向图中计算不同的无向边
发布时间:2020-05-23 08:03:46 所属栏目:MsSql 来源:互联网
导读:给定一个表,在这样的有向图中保持边: CREATE TABLE edges ( from_here int not null, to_there int not null) 获取特定节点的不同无向链接数量的最佳方法是什么?没有任何重复的定向边缘,也没有任何节点直接链接到自己,我只想避免计数重复的无向边(如(1,2)和
|
给定一个表,在这样的有向图中保持边: CREATE TABLE edges (
from_here int not null,to_there int not null
)
获取特定节点的不同无向链接数量的最佳方法是什么?没有任何重复的定向边缘,也没有任何节点直接链接到自己,我只想避免计数重复的无向边(如(1,2)和(2,1))两次. 这个工作,但NOT IN闻到对我不好: SELECT COUNT(*)
FROM edges
WHERE from_here = 1
OR (to_there = 1 AND from_here NOT IN (
SELECT to_there
FROM edges
WHERE from_here = 1
))
PostgreSQL特定的解决方案对此很好. 解决方法select count(*) from ( select to_there from edges where from_here = 1 union select from_here from edges where to_there = 1 ) as whatever (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
