原始表 :
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 and t.list is null
select * from test
/*
id typeid type class list
1 1 asd 13 NULL
2 2 erer 14 NULL
3 3 trt 15 NULL
4 1 asd 13 1
5 2 erer 14 3
6 3 trt 15 6
*/