日期:2014-05-16  浏览次数:20424 次

查找表内字段更新
原始表 :
id  typeid   type   class   list    
1   1        asd    13      
2   2        erer   14     
3   3        trt    15 
4            asd    13      1
5            erer   14      3
6            trt    15      6


更新typeid    根据type查找type相同 class相同 list为空 对应更新typeid   要求效果:

id  typeid   type   class   list    
1   1        asd    13      
2   2        erer   14     
3   3        trt    15 
4   1        asd    13      1
5   2        erer   14      3
6   3        trt    15      6
------解决方案--------------------
表名:Ta


update a
set typeid=b.typeid
from Ta as a inner join Ta as b on a.class=b.class and a.type=b.type and a.Typeid is null and b.typeid is not null
 
------解决方案--------------------
是这样吗:
--drop table test

create table test(id int, typeid int,type varchar(10),class int, list int)    

insert into test
select 1   ,1        ,'asd',    13,    null union all
select 2   ,2        ,'erer',   14,    null  union all
select 3   ,3        ,'trt',    15,    null union all
select 4   ,null     ,'asd',    13,      1 union all
select 5   ,null     ,'erer',   14,      3 union all
select 6   ,null     ,'trt',    15,      6
go

update test
set typeid = t.typeid
from test 
inner join test t
        on test.type = t.type and test.class = t.class