table_a:aid   num  
11     1
12     2
13     3
14     4
15     5
19     13
21     30
50     60
...    ...上面是数据库中的一张表,我现在想要得到的结果是: 给出一个数字,比如这个数字是12,那么我想通过一种算法找出和12相差最近的那个数字13,同样要是给的数字是55,那个得到是60这条记录大家对这个有什么好的idea,谢谢

解决方案 »

  1.   

    而且问题的关键不在于你的算法,在于表结构。你要是用BDB就好很多。说白了,是存储的效率问题。
      

  2.   

    你是问怎么实现吧
    第一步:
    把 select min(abs(num-要比较的数)) from a 结果赋给 m
    第二歩:
    select * from a where abs(num-要比较的数)=m在oracle写成一个sql的话
    select * from a where abs(num-要比较的数)= (select min(abs(num-要比较的数)) from a)
      

  3.   

    假设需要比较的数据为 n第一步: 查找等于该值的 数 如果有就返回该值  如果没有 执行第二步select num from table_a where  num=n第二步:查找比n大的最临近的一个数 和查找比n小的 最临近的一个数 判断他们与n差的绝对值  看哪个小 就是哪个
    select num from(select num from table_a where  num>n  order   by   num   asc) where rounum = 1;
    select num from(select num from table_a where  num<n  order   by   num   desc) where rounum = 1;