2009年(86)
分类: Mysql/postgreSQL
2009-10-18 15:33:58
For the earlier proof of concept we had valuable support from MySQL and MariaDB core developers such as Sergei Golubchik (Sun/MySQL), Timour Katchaounov (Sun/MySQL), Igor Babaev (Monty Program).
应用的小例子:
-- insert a few entries with connections (and multiple paths)
insert into foo (origid, destid) values (1,2), (2,3), (2,4), (4,5), (3,6), (5,6);
-- a regular table to join on to
insert into people values (1,"pearce"),(2,"hunnicut"),(3,"potter"),
(4,"hoolihan"),(5,"winchester"),(6,"mulcahy");
-- find us the shortest path from pearce (1) to mulcahy (6) please
select group_concat(people.name order by seq) as path
from foo join people on (foo.linkid=people.id)
where latch=1 and origid=1 and destid=6;
+--------+--------+--------------------------------+
| origid | destid | path |
+--------+--------+--------------------------------+
| 1 | 6 | pearce,hunnicut,potter,mulcahy |
+--------+--------+--------------------------------+
-- find us all people we can get to from potter (3)
select origid,group_concat(people.name order by seq) as destinations
from foo join people on (foo.linkid=people.id)
where latch=1 and origid=3;
+--------+----------------+
| origid | destinations |
+--------+----------------+
| 3 | mulcahy,potter |
+--------+----------------+
-- find us all people from where we can get to hoolihan (4)
select origid,group_concat(people.name order by seq) as origins
from foo join people on (foo.linkid=people.id)
where latch=1 and destid=4;
+--------+--------------------------+
| origid | origins |
+--------+--------------------------+
| 4 | hoolihan,hunnicut,pearce |
+--------+--------------------------+
So, there you have it. A graph (in this case a simple unidirectional tree, aka hierarchy) that looks like a table to us, as do the resultsets that have been computed.