首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求高难道的SQL更新语句!请高手帮帮忙解决方法

2012-01-22 
求高难道的SQL更新语句!请高手帮帮忙tableaid类别编码(char)13254323412565tableb类别名称类别编码书部费3

求高难道的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 送水费


3 书部费
4 送水费
5 杂费

(所影响的行数为 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

热点排行