我有一个图,它的节点是{A, B, C, D, ...}
,还有一个表,它指定了节点之间的有向边。
| node_1 | node_2 |
|-----------------|
| A | B |
| A | C |
| B | A |
| B | D |
| D | A |
如果从A
到B
有一条边,我们写为A ~ B
。因此,一行中有node_1 = A
和node_2 = B
,就意味着有A ~ B
。我区分以下几种关系:
A = B if A ~ B and B ~ A
A > B if A ~ B and not B ~ A
A < B if B ~ A and not A ~ B
如何检索与给定节点相邻的所有节点以及它们的关系类型?例如,在上表中查询A
应返回
| node | type |
|------|------|
| B | = | (because A ~ B and B ~ A)
| C | > | (because A ~ C and not C ~ A)
| D | < | (because D ~ A and not A ~ D)
2条答案
按热度按时间7gs2gvoe1#
这里有一种方法:
qlfbtfca2#
嗯......你可以将条件逻辑与聚合一起使用:
这里有一个db〈〉小提琴。
这似乎是最简单的解决方案,也可能是性能最好的解决方案。