日期:2014-05-18  浏览次数:20498 次

求高难道的SQL更新语句!请高手帮帮忙
tablea
id         类别编码(char)
1             3
2             54
3             23
4             12
5             65

tableb
类别名称         类别编码
书部费               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                             杂费


以上什么办法都可以,高手帮帮忙

------解决方案--------------------
update tablea
set 类别编码 = tableb.类别名称
from tablea,tableb
where charindex( ', ' + tablea.类别编码 + ', ' , tableb.类别编码) > 0

第一个有了.第二个就不写了吧.
------解决方案--------------------
select a.id,a.类别编码,(select 类别名称 from tableb b where charindex( ', '+a.类别编码+ ', ', ', '+b.类别编码+ ', ')> 0) as '类别名称 '
from tablea a
------解决方案--------------------
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 ')
go

if object_id( 'pubs..tbb ') is not null
drop table tbb
go

create 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.类别编码+ ', ') > 0

select * from tba

drop table tba,tbb

/*
id 类别编码
----------- ----------
1 书部费
2