我正在使用MariaDB https://mariadb.com/kb/en/library/oqgraph-storage-engine/的OQGRAPH存储引擎,我想实现节点之间的关系。
基于https://mariadb.com/kb/en/library/oqgraph-examples的示例数据:
表:people_relationships
origid destid relationship
1 2 friend
2 3 friend
3 4 friend
4 5 father
2 6 mother
5 6 sibling
查询所有destid节点1:
SELECT * FROM people_graph g
WHERE g.latch='dijkstras' AND g.origid=1;
我得到序列中每个节点的短路径:
latch origid destid weight seq linkid
dijkstras 1 NULL 4 6 5
dijkstras 1 NULL 3 5 4
dijkstras 1 NULL 2 4 6
dijkstras 1 NULL 2 3 3
dijkstras 1 NULL 1 2 2
dijkstras 1 NULL 0 1 1
但我也想知道,哪个节点会导致这种关系。换句话说:哪个节点是关系的起源(命名为previd)。
示例:
latch origid destid weight seq linkid previd
dijkstras 1 NULL 4 6 5 4
dijkstras 1 NULL 3 5 4 3
dijkstras 1 NULL 2 4 6 2
dijkstras 1 NULL 2 3 3 2
dijkstras 1 NULL 1 2 2 1
dijkstras 1 NULL 0 1 1 NULL
所以我可以将结果限制为一种特殊的关系:
SELECT * FROM people_graph g
JOIN people_relationships d ON d.destid = g.linkid AND d.origid = g.previd
WHERE g.latch='dijkstras'
AND g.origid=1
AND d.relationship='friend';
返回朋友和朋友的朋友,但没有家人。