一个表中某个字段的内容如下,存在多个并列的ID,
11,22,33,12,23
11
11,22
2,3
这些ID的属性表为:
ID
11 月
22 年
33 家
12 六
23 五
我希望得到的效果是:(将ID转换为对应的值)
月,年,家,六,五

月,年

请高人指点,我是刚开始学 ,很多简单的东西都不太会。多谢

解决方案 »

  1.   

    tba
    ID  classid   name
    1     1,2,3   西服 
    2     2,3    中山装
    3     1,3    名裤
    tbb 
    id   classname
    1     衣服
    2     上衣
    3     裤子我得的结果是
    id   classname            name
    1     衣服,上衣,裤子      西服 
    2          上衣,裤子     中山装
    3     衣服,裤子          名裤create table tba(ID int,classid varchar(20),name varchar(10))
    insert into tba values(1,'1,2,3','西服')
    insert into tba values(2,'2,3'  ,'中山装')
    insert into tba values(3,'1,3'  ,'名裤')
    create table tbb(ID varchar(10), classname varchar(10))
    insert into tbb values('1','衣服')
    insert into tbb values('2','上衣')
    insert into tbb values('3','裤子')
    go--第1种方法,创建函数来显示
    create function f_hb(@id varchar(10))
    returns varchar(1000)
    as
    begin
      declare @str varchar(1000)
      set @str=''
      select @str=@str+','+[classname] from tbb where charindex(','+cast(id as varchar)+',',','+@id+',')>0
      return stuff(@str,1,1,'')
    end
    go 
    select id,classid=dbo.f_hb(classid),name from tba
    drop function f_hb
    /*
    id          classid       name       
    ----------- ------------- ---------- 
    1           衣服,上衣,裤子 西服
    2           上衣,裤子      中山装
    3           衣服,裤子      名裤
    (所影响的行数为 3 行)
    */--第2种方法.update
    while(exists (select * from tba,tbb where charindex(tbb.id,tba.classid) >0))
    update tba
    set classid= replace(classid,tbb.id,tbb.classname)
    from tbb
    where charindex(tbb.id,tba.classid)>0
    select * from tba
    /*
    ID          classid              name       
    ----------- -------------------- ---------- 
    1           衣服,上衣,裤子       西服
    2           上衣,裤子            中山装
    3           衣服,裤子            名裤
    (所影响的行数为 3 行)
    */
    drop table tba,tbb
      

  2.   

    --分解字符串包含的信息值去另外一表查询相应的信息
    (爱新觉罗.毓华  2007-12-22  广东深圳)/*问题描述:
    需要将用户表中的用户组与信息表中的用户组对比.如果有相同的组则可查看该信息. 
    一个用户可能是几个用户组的成员.一条信息可能是几个用户组都可以查看. 表一 
    ID     usergroup     username 
    1      1,2,4         用户A 
    2      3             用户B 表二 
    id     usergroup     title   
    1      1,3,4         信息标题1 
    2      3             信息标题2 
    3      1,2,3         信息标题3 用户A可以看到   
    表二中的 
    信息标题1 
    信息标题3 用户B可以看到   
    表二中的 
    信息标题1 
    信息标题2 
    信息标题3
    */-------------------------------------------------------------
    --sql server 2000中用临时表的写法.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    go--建立一个辅助的临时表就可以了
    SELECT TOP 8000 id = identity(int,1,1) INTO tmp FROM syscolumns a, syscolumns b  
     
    select distinct c.username , d.title from
    (
      SELECT A.username,usergroup = SUBSTRING(A.usergroup, B.ID, CHARINDEX(',', A.usergroup + ',', B.ID) - B.ID) FROM tb A, tmp B WHERE SUBSTRING(',' + a.usergroup, B.id, 1) = ',' 
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA,tmp/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */--------------------------------------------------------------------
    --sql server 2000中不用临时表的SQL语句.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    goselect distinct c.username , d.title from
    (
      select a.username,usergroup=substring(a.usergroup,b.id,charindex(',',a.usergroup+',',b.id)-b.id)
      from tb a,(select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9  union select 10 union select 11 union select 12 union select 13 union select 14 union select 15) b
      where substring(','+a.usergroup,b.id,1)=','
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */-------------------------------------------------------------------
    --sql server 2005中的写法.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    goselect distinct c.username , d.title from
    (
      SELECT A.username, B.usergroup FROM(SELECT username, [usergroup] = CONVERT(xml,'<root><v>' + REPLACE([usergroup], ',', '</v><v>') + '</v></root>') FROM TB)A
      OUTER APPLY(SELECT usergroup = N.v.value('.', 'varchar(100)') FROM A.[usergroup].nodes('/root/v') N(v))B
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */
      

  3.   


    --换种方法,用递归:create table wb(id int,name varchar(50))
    insert into wb select 11,'月' 
    insert into wb select 22,'年' 
    insert into wb select 33,'家' 
    insert into wb select 12,'六'
    insert into wb select 23,'五'
    alter function w(@val varchar(50))
    returns varchar(100)
    as
    begin
    select @val=replace(@val,id,name) from wb
    if exists(select 1 from wb where charindex(ltrim(id),@val)>0)
    select @val=dbo.w(@val)
    else
    set @val=@val
    return @val
    enddeclare @a table(val varchar(50))
    insert into @a select '11,22,33,12,23' 
    insert into @a select '11' 
    insert into @a select '11,22' 
    insert into @a select '2,3' select dbo.w(val) from @a
      

  4.   

    谢谢各位给了这么多方法,采用了dawugui讲的第一个方法:创建函数来显示。但是又发现一个问题,就是分隔符有两个类型,一个是“,”,还有就是“|”,比如11,22|33,5|2  请问如果是两个或多个分隔符应该怎么处理呢。