原始表:
id    dictid    parentid    name
-----------------------------------
1          1                   a
2         11            1      b
3         12            1      c
4          2                   d
5         ds            2      e
6         gg            2      f
7          3                   g
8         sa            3      h
9         la            3      i查询后的表
id    dictid    parentid    name
-----------------------------------
1          1                   a
2         11            1      a
3         12            1      a
4          2                   d
5         ds            2      d
6         gg            2      d
7          3                   g
8         sa            3      g
9         la            3      g只有id是主键,其余全是字符串,
怎么实现此功能。
就是只要parentid=dictid的,name都显示为dictid主信息的name在线等

解决方案 »

  1.   

    /*
    原始表: 
    id    dictid    parentid    name 
    ----------------------------------- 
    1          1                  a 
    2        11            1      b 
    3        12            1      c 
    4          2                  d 
    5        ds            2      e 
    6        gg            2      f 
    7          3                  g 
    8        sa            3      h 
    9        la            3      i 查询后的表 
    id    dictid    parentid    name 
    ----------------------------------- 
    1          1                  a 
    2        11            1      a 
    3        12            1      a 
    4          2                  d 
    5        ds            2      d 
    6        gg            2      d 
    7          3                  g 
    8        sa            3      g 
    9        la            3      g 只有id是主键,其余全是字符串, 
    怎么实现此功能。 
    就是只要parentid=dictid的,name都显示为dictid主信息的name 
    */
    if exists (select * from dbo.sysobjects where id=object_id('tb00') and objectproperty(id,'istable')=1)
    drop table tb00
    go
    --判断是否存在所要创建的tb00表
    create table tb00(
    tbid int primary key,
    dictid varchar(20) not null,
    parentid varchar(20),
    tname varchar(20)
    )
    go
    --创建tb00表。中间有几个字段更改了下,避免为保留字。
    insert into tb00 values(1,'1',null,'a')
    insert into tb00 values(2,'11',1,'b')
    insert into tb00 values(3,'12',1,'c')
    insert into tb00 values(4,'2',null,'d')
    insert into tb00 values(5,'ds',2,'e')
    insert into tb00 values(6,'gg',2,'f')
    insert into tb00 values(7,'3',null,'g')
    insert into tb00 values(8,'sa',3,'h')
    insert into tb00 values(9,'la',3,'i')
    go
    --插入表值
    --select * from tb00
    select a.tbid,a.dictid,a.parentid,a.tname,(case when  a.parentid=b.parentid then b.ntname else a.tname end)as ntname from tb00 a
    left outer join (
    select a.tbid,b.parentid,a.tname as ntname from tb00 a inner join tb00 b on a.dictid=b.parentid group by a.tbid,b.parentid,a.tname
    )b on a.parentid=b.parentid
      

  2.   

    最后的查询中,为了对比,新的结果列在ntname中。在你自己需要的查询中,只需要稍微自己改动就是了。
      

  3.   

    select a.tbid,a.dictid,a.parentid,tname=(case  when parentid is null then tname else (select tname from tb00 where dictid=a.parentid) end ) from tb00 a
     
      

  4.   

    select a.tbid,a.dictid,a.parentid,tname=(case  when parentid is null then tname else (select tname from tb00 where dictid=a.parentid) end ) from tb00 a
     
      

  5.   

    select
    a.tbid,a.dictid,a.parentid,case when a.parentid is null then a.tname
     else (select tname from tb00 where a.parentid = dictid )
    end testName 
    from tb00 a