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

update多条记录!解决方案

2012-01-24 
update多条记录!有个表:idsortvalue1112132122232425......想把每个id中sort最小的,也就是1,updatevalue1

update多条记录!
有个表:
    id       sort     value  
      1           1          
      1           2    
      1           3
      2           1
      2           2
      2           3
      2           4
      2           5
    ...       ...
想把每个id中sort最小的,也就是1,update   value   =   1,最大的,update   value=2,其余的update   value   =   4,能用一句sql写出来吗?


[解决办法]
update test set value=(select decode(test.sort - a.min_s,0,1,decode(test.sort - a.max_s,0,2,4)) from (select id,min(sort) min_s,max(sort) max_s from test group by id) a where a.id=test.id)
[解决办法]
create table test(id number,sort number,value number)

insert into test(id,sort) values(1,1);
insert into test(id,sort) values(1,2);
insert into test(id,sort) values(1,3);
insert into test(id,sort) values(2,1);
insert into test(id,sort) values(2,2);
insert into test(id,sort) values(2,3);
insert into test(id,sort) values(2,4);
insert into test(id,sort) values(2,5);
commit;

select * from test

update test a
set value = (select decode(b.sort, c.max, 2, c.min, 1, 4)
from test b,
(select max(sort) max, min(sort) min from test) c
where b.id = a.id
and b.sort = a.sort)

热点排行