表a:
ip   name
1   bear
2   lime
3   gear
4   peer
表b:
source ip  destination ip
1            2
1            3
2            1
2            3
3            4
............
希望达到的是这样的表C:
source ip  (s)name  destination ip  (d)name
1          bear          2           lime   
1          bear          3           gear
2          bear          1           bear
2          lime          3           gear
3          gear          4           peer
...........请高手指教,跪谢!

解决方案 »

  1.   

    declare @ta table(ip int, name varchar(10))
    insert @ta select 1   ,'bear'
    insert @ta select 2   ,'lime'
    insert @ta select 3   ,'gear'
    insert @ta select 4   ,'peer'declare @tb table([source ip] int, [destination ip] int)
    insert @tb select 1            ,2
    insert @tb select 1            ,3
    insert @tb select 2            ,1
    insert @tb select 2            ,3
    insert @tb select 3            ,4select b.[source ip],a.name,b.[destination ip],c.name from @ta a,@ta c,@tb b where a.ip=b.[source ip] and c.ip=b.[destination ip]
    source ip   name       destination ip name       
    ----------- ---------- -------------- ---------- 
    2           lime       1              bear
    1           bear       2              lime
    1           bear       3              gear
    2           lime       3              gear
    3           gear       4              peer(所影响的行数为 5 行)
      

  2.   

    select b.sourceip,a.name,b.destinationip,c.name from b join a a on b.sourceip=a.ip
    join a c on b.destinationip=c.ip
      

  3.   

    create table a(ip int,name varchar(20))
    insert a select 1,'bear'
    union all select 2,'lime'
    union all select 3,'gear'
    union all select 4,'peer'create table b(source int,ip1 varchar(20),destination int,ip2 varchar(20))
    insert b select 1,null,2,null
    union all select 1,null,3,null
    union all select 2,null,1,null
    union all select 2,null,3,null
    union all select 3,null,4,nullselect z.source,x.name,z.destination,y.name from a x,a y,b z where x.ip=z.source and y.ip=z.destination
      

  4.   

    先谢谢楼上几位大人的指点,我好像没说清楚,希望得到的是一张表(因为以后还要在这张表理查东西),用select语句是不是只能列出来而不能生成表啊?(我小白,不太清楚)
    还有表a和b都是非常大的表手动逐个输入数据比较困难。
      

  5.   

    select z.source,x.name,z.destination,y.name into 表 from a x,a y,b z where x.ip=z.source and y.ip=z.destination
      

  6.   

    declare @ta table(ip int, name varchar(10))
    insert @ta select 1,'bear'
    insert @ta select 2,'lime'
    insert @ta select 3,'gear'
    insert @ta select 4,'peer'declare @tb table([source ip] int, [destination ip] int)
    insert @tb select 1,2
    insert @tb select 1,3
    insert @tb select 2,1
    insert @tb select 2,3
    insert @tb select 3,4select b.[source ip],a.name,b.[destination ip],c.name into 新表 from @ta a,@ta c,@tb b where a.ip=b.[source ip] and c.ip=b.[destination ip]如果要生成表已经存在,可以只添加数据,使用下面的语句:
    insert into 新表 select b.[source ip],a.name,b.[destination ip],c.name from @ta a,@ta c,@tb b where a.ip=b.[source ip] and c.ip=b.[destination ip]
      

  7.   

    create table #b([source ip] int,[destination ip] int)
    create table #a([ip] int,[name] char(5))
    insert #a select 1,   'bear'
    insert #a select 2,   'lime'
    insert #a select 3,   'gear'
    insert #a select 4,   'peer'
    insert #b select 1,            2
    insert #b select 1,            3
    insert #b select 2,            1
    insert #b select 2,            3
    insert #b select 3,            4
    select #b.[source ip] as [source ip],(select #a.[name] from #a where #b.[source ip]=#a.[ip]) as [source name],
    #b.[destination ip] as [destination ip],(select #a.[name] from #a where #b.[destination ip]=#a.[ip]) as [destination name]
    into [#你要的表]
    from #b
    select * from [#你要的表]~~~
    source ip  source name  destination ip  destination na
    1       bear   2  lime 
    1  bear   3   gear 
    2  lime   1  bear 
    2  lime   3  gear 
    3  gear   4  peer