表a
  id  name  pid
  18 商家    0      
  11 网吧    0 
  17 经理    11 
  16 服务员  11  
  15 收银员  11 
  14 技术主管 11
  13 技术员  11 
  12 网管   11 
  19 硬件   18 
  24 装修   18 
  23 增值   18 
  22 桌椅   18   
  21 服务器 19  
  25 办公桌 22  
  26 网吧转让 23 
  27 简单装修 24 表b
name a.id
11    21
12    21
13    25
14    25
15    26
17    26
18    27怎样显示为这样的形式:
a.name  b.name1 b.name2 a.id 
11      硬件     服务器  21
12      硬件     服务器  21
13      桌椅     办公桌  25
14      桌椅     办公桌  25
15      增值     网吧转让 26
17      增值     网吧转让 26
18      装修     简单装修 27
谢谢

解决方案 »

  1.   

    -------------------------------------
    --  Author : liangCK 梁爱兰
    --  Comment: 小梁 爱 兰儿
    --  Date   : 2009-11-25 16:39:11
    -------------------------------------
     
    --> 生成测试数据: @tb1
    DECLARE @tb1 TABLE (id int,name varchar(8),pid int)
    INSERT INTO @tb1
    SELECT 18,'商家',0 UNION ALL
    SELECT 11,'网吧',0 UNION ALL
    SELECT 17,'经理',11 UNION ALL
    SELECT 16,'服务员',11 UNION ALL
    SELECT 15,'收银员',11 UNION ALL
    SELECT 14,'技术主管',11 UNION ALL
    SELECT 13,'技术员',11 UNION ALL
    SELECT 12,'网管',11 UNION ALL
    SELECT 19,'硬件',18 UNION ALL
    SELECT 24,'装修',18 UNION ALL
    SELECT 23,'增值',18 UNION ALL
    SELECT 22,'桌椅',18 UNION ALL
    SELECT 21,'服务器',19 UNION ALL
    SELECT 25,'办公桌',22 UNION ALL
    SELECT 26,'网吧转让',23 UNION ALL
    SELECT 27,'简单装修',24
     
    --> 生成测试数据: @tb2
    DECLARE @tb2 TABLE (name int,id int)
    INSERT INTO @tb2
    SELECT 11,21 UNION ALL
    SELECT 12,21 UNION ALL
    SELECT 13,25 UNION ALL
    SELECT 14,25 UNION ALL
    SELECT 15,26 UNION ALL
    SELECT 17,26 UNION ALL
    SELECT 18,27--SQL查询如下:SELECT A.name,C.name AS name1,B.name AS name2,A.id
    FROM @tb2 AS A 
        LEFT JOIN @tb1 AS B
    ON A.id = B.id 
        LEFT JOIN @tb1 AS C
    ON B.pid = C.id/*
    name        name1    name2    id
    ----------- -------- -------- -----------
    11          硬件       服务器      21
    12          硬件       服务器      21
    13          桌椅       办公桌      25
    14          桌椅       办公桌      25
    15          增值       网吧转让     26
    17          增值       网吧转让     26
    18          装修       简单装修     27(7 行受影响)
    */
      

  2.   

    由于你只显示2级.所以直接join就行了.
      

  3.   


    select a.name,b1.name,b2.name,a.id
    from a
    inner join b b1 on a.id=b1.id
    inner join b b2 on b1.pid=b2.id
      

  4.   

    -- =============================================
    -- Author:      T.O.P
    -- Create date: 2009/11/25
    -- Version:     SQL SERVER 2005
    -- =============================================
    if object_id('[a]') is not null drop table [a]
    go
    create table [a]([id] int,[name] varchar(8),[pid] int)
    insert [a]
    select 18,'商家',0 union all
    select 11,'网吧',0 union all
    select 17,'经理',11 union all
    select 16,'服务员',11 union all
    select 15,'收银员',11 union all
    select 14,'技术主管',11 union all
    select 13,'技术员',11 union all
    select 12,'网管',11 union all
    select 19,'硬件',18 union all
    select 24,'装修',18 union all
    select 23,'增值',18 union all
    select 22,'桌椅',18 union all
    select 21,'服务器',19 union all
    select 25,'办公桌',22 union all
    select 26,'网吧转让',23 union all
    select 27,'简单装修',24if object_id('[b]') is not null drop table [b]
    go
    create table [b]([name] int,[id] int)
    insert [b]
    select 11,21 union all
    select 12,21 union all
    select 13,25 union all
    select 14,25 union all
    select 15,26 union all
    select 17,26 union all
    select 18,27SELECT B.name , A1.name,A.name , a.id
    FROM A INNER JOIN B ON A.id = B.id
       INNER JOIN A A1 ON A.PID = A1.ID
    order by b.name
    --测试结果:
    /*
    name        name     name     id
    ----------- -------- -------- -----------
    11          硬件       服务器      21
    12          硬件       服务器      21
    13          桌椅       办公桌      25
    14          桌椅       办公桌      25
    15          增值       网吧转让     26
    17          增值       网吧转让     26
    18          装修       简单装修     27(7 row(s) affected)
    */
      

  5.   

    ---测试数据---
    if object_id('[a]') is not null drop table [a]
    go
    create table [a]([id] int,[name] varchar(8),[pid] int)
    insert [a]
    select 18,'商家',0 union all
    select 11,'网吧',0 union all
    select 17,'经理',11 union all
    select 16,'服务员',11 union all
    select 15,'收银员',11 union all
    select 14,'技术主管',11 union all
    select 13,'技术员',11 union all
    select 12,'网管',11 union all
    select 19,'硬件',18 union all
    select 24,'装修',18 union all
    select 23,'增值',18 union all
    select 22,'桌椅',18 union all
    select 21,'服务器',19 union all
    select 25,'办公桌',22 union all
    select 26,'网吧转让',23 union all
    select 27,'简单装修',24
    if object_id('[b]') is not null drop table [b]
    go
    create table [b]([name] int,[a.id] int)
    insert [b]
    select 11,21 union all
    select 12,21 union all
    select 13,25 union all
    select 14,25 union all
    select 15,26 union all
    select 17,26 union all
    select 18,27
     
    ---查询---
    select 
      b.name,
      c.name as name1,
      a.name as name2,
      b.[a.id]
    from b
    left join a  on a.id=b.[a.id]
    left join a as c on c.id=a.pid
    ---结果---
    name        name1    name2    a.id        
    ----------- -------- -------- ----------- 
    11          硬件       服务器      21
    12          硬件       服务器      21
    13          桌椅       办公桌      25
    14          桌椅       办公桌      25
    15          增值       网吧转让     26
    17          增值       网吧转让     26
    18          装修       简单装修     27(所影响的行数为 7 行)
      

  6.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-11-25 16:42:06
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([id] int,[name] varchar(8),[pid] int)
    insert [a]
    select 18,'商家',0 union all
    select 11,'网吧',0 union all
    select 17,'经理',11 union all
    select 16,'服务员',11 union all
    select 15,'收银员',11 union all
    select 14,'技术主管',11 union all
    select 13,'技术员',11 union all
    select 12,'网管',11 union all
    select 19,'硬件',18 union all
    select 24,'装修',18 union all
    select 23,'增值',18 union all
    select 22,'桌椅',18 union all
    select 21,'服务器',19 union all
    select 25,'办公桌',22 union all
    select 26,'网吧转让',23 union all
    select 27,'简单装修',24
    --> 测试数据:[b]
    if object_id('[b]') is not null drop table [b]
    go 
    create table [b]([name] int,[id] int)
    insert [b]
    select 11,21 union all
    select 12,21 union all
    select 13,25 union all
    select 14,25 union all
    select 15,26 union all
    select 17,26 union all
    select 18,27
    --------------开始查询--------------------------
    select
       b.name,a1.name as name1,a2.name as name2,a1.id 
    from
       b 
    inner join
       a a1 on b.id=a1.id 
    inner join
       a a2 on a1.pid=a2.id
    ----------------结果----------------------------
    /*name        name1    name2    id
    ----------- -------- -------- -----------
    11          服务器      硬件       21
    12          服务器      硬件       21
    18          简单装修     装修       27
    15          网吧转让     增值       26
    17          网吧转让     增值       26
    13          办公桌      桌椅       25
    14          办公桌      桌椅       25(7 行受影响)*/
      

  7.   

    --> Title  : Generating test data [ta]
    --> Author : 
    --> Date   : 2009-11-25 
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta] (id int,name nvarchar(8),pid int)
    insert into [ta]
    select 18,'商家',0 union all
    select 11,'网吧',0 union all
    select 17,'经理',11 union all
    select 16,'服务员',11 union all
    select 15,'收银员',11 union all
    select 14,'技术主管',11 union all
    select 13,'技术员',11 union all
    select 12,'网管',11 union all
    select 19,'硬件',18 union all
    select 24,'装修',18 union all
    select 23,'增值',18 union all
    select 22,'桌椅',18 union all
    select 21,'服务器',19 union all
    select 25,'办公桌',22 union all
    select 26,'网吧转让',23 union all
    select 27,'简单装修',24
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (name int,id int)
    insert into [tb]
    select 11,21 union all
    select 12,21 union all
    select 13,25 union all
    select 14,25 union all
    select 15,26 union all
    select 17,26 union all
    select 18,27
    SELECT A.name,
           C.name AS name1,
           B.name AS name2,
           A.id
    FROM tb AS A , TA AS B ,TA AS C
    WHERE A.id = B.id    
    AND B.pid = C.id
    /*
    name        name1    name2    id
    ----------- -------- -------- -----------
    11          硬件       服?器      21
    12          硬件       服?器      21
    18          ?修       ???修     27
    15          增值       网吧??     26
    17          增值       网吧??     26
    13          桌椅       ?公桌      25
    14          桌椅       ?公桌      25(7 個資料列受到影響)
    */
      

  8.   

    --> 测试数据:@table1
    declare @table1 table([id] int,[name] varchar(8),[pid] int)
    insert @table1
    select 18,'商家',0 union all
    select 11,'网吧',0 union all
    select 17,'经理',11 union all
    select 16,'服务员',11 union all
    select 15,'收银员',11 union all
    select 14,'技术主管',11 union all
    select 13,'技术员',11 union all
    select 12,'网管',11 union all
    select 19,'硬件',18 union all
    select 24,'装修',18 union all
    select 23,'增值',18 union all
    select 22,'桌椅',18 union all
    select 21,'服务器',19 union all
    select 25,'办公桌',22 union all
    select 26,'网吧转让',23 union all
    select 27,'简单装修',24
    --> 测试数据:@table2
    declare @table2 table([name] int,[id] int)
    insert @table2
    select 11,21 union all
    select 12,21 union all
    select 13,25 union all
    select 14,25 union all
    select 15,26 union all
    select 17,26 union all
    select 18,27select b.name,
    (select name from @table1 where id = (select pid from @table1 where id = b.id))as name2,
    a.name,b.id from @table1 a
    join @table2 b on
    b.id = a.id--结果
    ---------------------------
    11 硬件 服务器 21
    12 硬件 服务器 21
    13 桌椅 办公桌 25
    14 桌椅 办公桌 25
    15 增值 网吧转让 26
    17 增值 网吧转让 26
    18 装修 简单装修 27