日期:2014-05-17  浏览次数:20765 次

史上最坑爹的update 求答案
有个表
A(ID number not null,
  age number,
  last_age number,
  OK number)

所谓last_age就是(比本人小的,最接近本人年龄的某人)的年龄,
这个某人的OK字段不能等于0的 不能等于null 也不能是0


现在要求更新last_age  
求update还要
高效!!!!

------解决方案--------------------
SQL code

update a set last_age=
(select b.age from a b where a.id!=b.id and ok is not null and ok !=0 and a.age>b.age
rownum=1 order by a.age-b.age )

------解决方案--------------------
排了序了更新,
判断-1跟当前的差值跟+1跟当前的差值就行了
------解决方案--------------------
那3岁的呢 没得更小的了 不更新?
------解决方案--------------------
探讨
SQL code


update a set last_age=
(select b.age from a b where a.id!=b.id and ok is not null and ok !=0 and a.age>b.age
rownum=1 order by a.age-b.age )