表A:
id    dempId    dempName    parentid
1     2         销售部      0 
2     3         采购部      0
3     21        销售一部    2
4     6         采购一部    3这张表Id是主键,自增的。dempId是部门编号,dempName是部门名称,parentid是部门的父级dempId。现在我想查出下面这样的东西来:
id    dempId    dempName    parentName
1     2         销售部      无 
2     3         采购部      无
3     21        销售一部    销售部
4     6         采购一部    采购部请问要怎么写这条sql语句呢?
因为我前台用的是jquery ajax做的,并且没有查出parentName,如果要查出parentName,还需要根据查出的dempId再做一次ajax请求获取。但如果这样数据多的话,觉得效率很低。所以想问问大家能不能一下直接查询出来。

解决方案 »

  1.   

    ; with f as
    (
    select * from tb t where not exists(select 1 from tb where dempId=t.parentid
    union all
    select a.* from tb a join f on a.parentid=f.dempId
    )
    select
     a.id ,a.dempId a.dempName,isnull(b.dempName,'无') as parentName
    from
     tb a left join f b
    on
     a.id=b.id
    and
     a.dempId=b.dempId
      

  2.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-10-19 23:31:15
    -- Version:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([id] int,[dempId] int,[dempName] varchar(8),[parentid] int)
    insert [tb]
    select 1,2,'销售部',0 union all
    select 2,3,'采购部',0 union all
    select 3,21,'销售一部',2 union all
    select 4,6,'采购一部',3
    --------------开始查询--------------------------
    ; with f as
    (
    select * from tb t where not exists(select 1 from tb where dempId=t.parentid)
    union all
    select a.* from tb a join f on a.parentid=f.dempId
    )
    select
     a.id ,a.dempId ,a.dempName,isnull(b.dempName,'无') as parentName
    from
     tb a left join f b
    on
     a.parentid=b.dempId
    ----------------结果----------------------------
    /* id          dempId      dempName parentName
    ----------- ----------- -------- ----------
    1           2           销售部      无
    2           3           采购部      无
    3           21          销售一部     销售部
    4           6           采购一部     采购部(4 行受影响)*/
      

  3.   

    --> --> (Roy)生成測試數據
     
    if not object_id('Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([id] int,[dempId] int,[dempName] nvarchar(4),[parentid] int)
    Insert #T
    select 1,2,N'销售部',0 union all
    select 2,3,N'采购部',0 union all
    select 3,21,N'销售一部',2 union all
    select 4,6,N'采购一部',3
    Go
    Select [id], [dempId],[dempName],
    [parentName]=isnull((select [dempName] from #T where [dempId]=a.[parentid]),'无')
    from #T as a
    /*
    id dempId dempName parentName
    1 2 销售部 无
    2 3 采购部 无
    3 21 销售一部 销售部
    4 6 采购一部 采购部
    */
      

  4.   

    --如果只有两层----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-10-19 23:31:15
    -- Version:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([id] int,[dempId] int,[dempName] varchar(8),[parentid] int)
    insert [tb]
    select 1,2,'销售部',0 union all
    select 2,3,'采购部',0 union all
    select 3,21,'销售一部',2 union all
    select 4,6,'采购一部',3
    --------------开始查询--------------------------
    select
     a.id ,a.dempId ,a.dempName,isnull(b.dempName,'无') as parentName
    from
     tb a left join tb b
    on
     a.parentid=b.dempId
    ----------------结果----------------------------
    /* id          dempId      dempName parentName
    ----------- ----------- -------- ----------
    1           2           销售部      无
    2           3           采购部      无
    3           21          销售一部     销售部
    4           6           采购一部     采购部(4 行受影响)*/