表结构如下:
empno  ename  manege  sex
 01     张三    02     男
 02     李四    04     女
 03     王五           女
 04     赵六    03       男
 05     刘七    01    男
问题:
1.查询 员工,性别,上司姓名;
2.员工如无上司,显示“无”;
3.根据性别倒序排列;实在想不出来解决的办法。请精通数据库查询的老师们帮帮我吧。共同学习

解决方案 »

  1.   

    *
    标题:查询各节点的父路径函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)  
    时间:2008-05-12
    地点:广东深圳
    *//*
    原始数据及要求结果如下:
    --食品 
      --水果 
        --香蕉 
        --苹果    
      --蔬菜 
        --青菜
    id          pid         name                 
    ----------- ----------- -------------------- 
    1           0           食品
    2           1           水果
    3           1           蔬菜
    4           2           香蕉
    5           2           苹果
    6           3           青菜要求得到各节点的父路径即如下结果:
    id  pid name  路径                         
    --- --- ----- ---------------
    1   0   食品  食品
    2   1   水果  食品,水果
    3   1   蔬菜  食品,蔬菜
    4   2   香蕉  食品,水果,香蕉
    5   2   苹果  食品,水果,苹果
    6   3   青菜  食品,蔬菜,青菜 
    */create table tb (id int , pid int , name nvarchar(20)) 
    insert into tb values(1 , 0 , '食品')
    insert into tb values(2 , 1 , '水果')
    insert into tb values(3 , 1 , '蔬菜')
    insert into tb values(4 , 2 , '香蕉')
    insert into tb values(5 , 2 , '苹果')
    insert into tb values(6 , 3 , '青菜')
    go--查询各节点的父路径函数
    create function f_pid(@id int) returns varchar(100)
    as
    begin
      declare @re_str as varchar(100)
      set @re_str = ''
      select @re_str = name from tb where id = @id
      while exists (select 1 from tb where id = @id and pid <> 0)
        begin
          select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
        end
      return @re_str
    end
    goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
    drop function f_pidSQL code
    /*
    标题:查询所有节点及其所有子节点的函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2009-04-12
    地点:广东深圳
    */--生成测试数据 
    create table tb(id varchar(10),pid varchar(10)) 
    insert into tb select 'a', null 
    insert into tb select 'b', 'a' 
    insert into tb select 'c', 'a' 
    insert into tb select 'd', 'b' 
    insert into tb select 'e', 'b' 
    insert into tb select 'f', 'c' 
    insert into tb select 'g', 'c' 
    go --创建用户定义函数 
    create function f_getchild(@id varchar(10)) returns varchar(8000) 
    as 
    begin 
      declare @i int , @ret varchar(8000) 
      declare @t table(id varchar(10) , pid varchar(10) , level int) 
      set @i = 1 
      insert into @t select id , pid , @i from tb where id = @id 
      while @@rowcount <> 0 
      begin 
        set @i = @i + 1 
        insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
      end 
      select @ret = isnull(@ret , '') + id + ',' from @t 
      return left(@ret , len(@ret) - 1)
    end 
    go --执行查询 
    select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id
    go --输出结果 
    /* 
    id         children     
    ---------- -------------
    a          a,b,c,d,e,f,g
    b          b,d,e
    c          c,f,g
    d          d
    e          e
    f          f
    g          g(所影响的行数为 7 行)*/ --删除测试数据 
    drop function f_getchild 
    drop table tbBOM的