给定一个表,在这样的有向图中保持边:
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 ))
解决方法
select count(*) from ( select to_there from edges where from_here = 1 union select from_here from edges where to_there = 1 ) as whatever