1
我有存储在表中的大图的邻接列表。MySQL:提取子图?
v1 | v2
1 | 2
1 | 3
1 | 4
2 | 5
3 | 5
3 | 6
4 | 7
7 | 9
5 | 10
我试图提取图2跳远离说顶点1,使其返回从上面的列表中的所有边缘除(7,9)和(5,10)。我管理这个:
SELECT * FROM stub_graph WHERE v1 IN (SELECT v1 FROM stub_graph WHERE v1=1 UNION select v2 FROM stub_graph WHERE v1=1) ;
我不是特别满意我的原因有两个解决方案:
- 的
IN
- 这是能够 提取链接,如1-2和2存在-5 但我无法提取链接 ,如6-7。
有没有很好的方法来做到这一点?
万一有人有兴趣在创建表时,这里的SQL代码:
create table stub_graph(v1 int(11), v2 int(11));
insert into stub_graph VALUES(1,2);
insert into stub_graph VALUES(1,3);
insert into stub_graph VALUES(1,4);
insert into stub_graph VALUES(2,5);
insert into stub_graph VALUES(3,5);
insert into stub_graph VALUES(3,6);
insert into stub_graph VALUES(4,7);
insert into stub_graph VALUES(6,7);
insert into stub_graph VALUES(7,9);
insert into stub_graph VALUES(5,10);