给定一个表,在这样的有向图中保持边:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。