我们业务里有一张表存储的是图关系结构,今天,我们需要查询出和某个节点有联系的所有节点。图是有向图。
业务抽象出的表结构很简单:
create table if not exists tests(
id serial not null,
point1 int not null,
point2 int not null,
primary key(id)
);
其中,我们定义point1
是起始点,point2
是终点。举个例子:(1, 2)
代表从1
节点到2
节点。
然后我们模拟一些测试数据:
insert into tests (point1, point2) values(1, 4),
(1, 5),
(4, 6),
(5, 6),
(2, 5),
(2, 7),
(7, 8),
(3, 9);
这些节点对应的图如下:
1 2 3
+ + +
| | |
+----------&