网络信息的SQL查询

网络信息的SQL查询,sql,database,networking,social-networking,Sql,Database,Networking,Social Networking,我有一个有14个节点的网络。(如1-14) 我不知道如何显示网络,但我可以提供包含网络信息的数据库表。网络信息如下: Nodes Connected to the nodes 1 2,5 2 1,3,4,5 3 2,4 4 2,3,5 5 1,2,4 6 5,11,12 7 4,8,9 8 7 9 4,7,

我有一个有14个节点的网络。(如1-14) 我不知道如何显示网络,但我可以提供包含网络信息的数据库表。网络信息如下:

Nodes Connected to the nodes 1 2,5 2 1,3,4,5 3 2,4 4 2,3,5 5 1,2,4 6 5,11,12 7 4,8,9 8 7 9 4,7,10,14 10 9,11 11 6,10 12 6,13 13 12,14 14 9,13 连接到节点的节点 1 2,5 2 1,3,4,5 3 2,4 4 2,3,5 5 1,2,4 6 5,11,12 7 4,8,9 8 7 9 4,7,10,14 10 9,11 11 6,10 12 6,13 13 12,14 14 9,13 现在,在这种情况下,我需要所有节点的信息,这些节点距离节点8有2个跳(假设)

节点8-节点7(第一跳) 节点7-节点4,8,9(第二跳) 因此,解决方案是“node4和node9距离node8有2跳

是否有SQL查询有助于解决此问题?它可能不只是始终为2个跃点。如果问题为3个跃点,我们需要进一步执行以下步骤:

节点4,8,9连接到2,3,5,7,4,10,14

p=>2,3,5,7,10,14(我们排除4,因为它已经2跳远离NODE8,我们将考虑最短路径)是3跳远离NODE8。< /P> 是否有SQL查询可以处理上表的这种情况? 如果我有什么不清楚的地方,请告诉我。

你可以试试

模式:

create table tNodes (
  id int
  ,connectedto int
)

insert tNodes values(1,2)
insert tNodes values(1,5)
insert tNodes values(2,1)
insert tNodes values(2,3)
insert tNodes values(2,4)
insert tNodes values(2,5)
insert tNodes values(3,2)
insert tNodes values(3,4)
insert tNodes values(4,2)
insert tNodes values(4,3)
insert tNodes values(4,5)
insert tNodes values(5,1)
insert tNodes values(5,2)
insert tNodes values(5,4)
insert tNodes values(6,5)
insert tNodes values(6,11)
insert tNodes values(6,12)
insert tNodes values(7,4)
insert tNodes values(7,8)
insert tNodes values(7,9)
insert tNodes values(8,7)
insert tNodes values(9,4)
insert tNodes values(9,7)
insert tNodes values(9,10)
insert tNodes values(9,14)
insert tNodes values(10,9)
insert tNodes values(10,11)
insert tNodes values(11,6)
insert tNodes values(11,10)
insert tNodes values(12,6)
insert tNodes values(12,13)
insert tNodes values(13,12)
insert tNodes values(13,14)
insert tNodes values(14,9)
insert tNodes values(14,13)
查询:

declare @id int = 8
declare @iHops int = 3
declare @iCnt int = 0

create table #tf (id int)
create table #tf0 (id int)
create table #tt (id int)

insert #tf values (@id)

while @iCnt<@iHops
begin

insert
    #tt (id)
  select distinct
    connectedto
  from
    tNodes
  where
    id in (select id from #tf)
    and
    connectedto not in (select id from #tf0)

  delete #tf
  insert #tf (id) select id from #tt
  insert #tf0 (id) select id from #tt
  delete #tt

  set @iCnt = @iCnt + 1
end

/*
select * from #tt
union all
select -1
union all
*/
select * from #tf
/*
union all
select -1
union all
select * from #tf0
*/
drop table #tf0
drop table #tf
drop table #tt

因为4和7也包括在内。(7距离8有1跳,4距离8有2跳)。

你真的想在SQL中这样做吗?@Anamika:解决方案中有帮助,或者在这方面还有一个悬而未决的问题?
declare @id int = 8
declare @iHops int = 3
declare @iCnt int = 0

create table #tf (id int)
create table #tf0 (id int)
create table #tt (id int)

insert #tf values (@id)

while @iCnt<@iHops
begin

insert
    #tt (id)
  select distinct
    connectedto
  from
    tNodes
  where
    id in (select id from #tf)
    and
    connectedto not in (select id from #tf0)

  delete #tf
  insert #tf (id) select id from #tt
  insert #tf0 (id) select id from #tt
  delete #tt

  set @iCnt = @iCnt + 1
end

/*
select * from #tt
union all
select -1
union all
*/
select * from #tf
/*
union all
select -1
union all
select * from #tf0
*/
drop table #tf0
drop table #tf
drop table #tt
ID
2
3
5
10
14