tablea
id    类别编码(char)
1      3
2      54
3      23
4      12
5      65tableb
类别名称    类别编码
书部费       3,23
送水费       54,12
杂费          65想要的结果1,
用update 语句将tablea 类别编码更新为tablab的类别名称
tablea
id    类别编码(char)
1      书部费
2      送水费
3      书部费
4      送水费
5      杂费想要的结果2,id    类别编码(char)  类别名称
1      3               书部费
2      54              送水费 
3      23              书部费
4      12              送水费
5      65              杂费
以上什么办法都可以,高手帮帮忙

解决方案 »

  1.   

    还有想要的结果.不够分再加
    把tableb 插分成这样
    tableb
    类别名称    类别编码
    书部费       3
    书部费       23
    送水费       54
    送水费       12
    杂费         65类别编码会有很多个编码一起的,不会就两个数组成
      

  2.   

    update tablea
    set 类别编码 = tableb.类别名称
    from tablea,tableb
    where charindex(',' + tablea.类别编码 + ',' , tableb.类别编码) > 0第一个有了.第二个就不写了吧.
      

  3.   

    select a.id,a.类别编码,(select 类别名称 from tableb b where charindex(','+a.类别编码+',',','+b.类别编码+',')>0) as '类别名称'
    from tablea a
      

  4.   

    update tablea
    set 类别编码 = tableb.类别名称
    from tablea,tableb
    where charindex(',' + tablea.类别编码 + ',' , ','+tableb.类别编码+',') > 0第一个有了.第二个就不写了吧.
      

  5.   

    declare @ta table(id int,类别编码 varchar(8))
    insert into @ta select 
    1,      '3' union all select 
    2,      '54' union all select 
    3,      '23' union all select 
    4,      '12' union all select 
    5,      '65'declare @tb table(类别名称 varchar(16),类别编码 varchar(32))
    insert into @tb select 
    '书部费',       '3,23'  union all select 
    '送水费',       '54,12' union all select 
    '杂费'  ,        '65'select a.id,b.类别名称 from @ta a inner join @tb b on charindex(a.类别编码,b.类别编码)>0 order by a.id
    select a.*,b.类别名称 from @ta a inner join @tb b on charindex(a.类别编码,b.类别编码)>0 order by a.id
      

  6.   

    if object_id('pubs..tba') is not null
       drop table tba
    go
    create table tba(id int,类别编码 varchar(10))
    insert into tba(id,类别编码) values(1,      '3')
    insert into tba(id,类别编码) values(2,      '54')
    insert into tba(id,类别编码) values(3,      '23')
    insert into tba(id,类别编码) values(4,      '12')
    insert into tba(id,类别编码) values(5,      '65')
    goif object_id('pubs..tbb') is not null
       drop table tbb
    gocreate table tbb(类别名称 varchar(10),类别编码 varchar(10))
    insert into tbb(类别名称,类别编码) values('书部费',       '3,23')
    insert into tbb(类别名称,类别编码) values('送水费',       '54,12')
    insert into tbb(类别名称,类别编码) values('杂费'  ,        '65')
    go
    update tba
    set 类别编码 = tbb.类别名称
    from tba,tbb
    where charindex(',' + tba.类别编码 + ',' , ','+tbb.类别编码+',') > 0select * from tbadrop table tba,tbb/*
    id          类别编码       
    ----------- ---------- 
    1           书部费
    2           送水费
    3           书部费
    4           送水费
    5           杂费(所影响的行数为 5 行)*/
      

  7.   

    declare @ta table(id int,类别编码 varchar(8))
    insert into @ta select 
    1,      '3' union all select 
    2,      '54' union all select 
    3,      '23' union all select 
    4,      '12' union all select 
    5,      '65'declare @tb table(类别名称 varchar(16),类别编码 varchar(32))
    insert into @tb select 
    '书部费',       '3,23'  union all select 
    '送水费',       '54,12' union all select 
    '杂费'  ,        '65'select a.id,b.类别名称 from @ta a inner join @tb b on charindex(a.类别编码+',',b.类别编码+',')>0 order by a.id
    select a.*,b.类别名称 from @ta a inner join @tb b on charindex(a.类别编码+',',b.类别编码+',')>0 order by a.id