有一个表t,含有一个字段a
a
4.3
7
8
5.3
2
8
需要产生下面的表
a b
4.3 -0.39
7 -0.125
8 0.59
5.3 1.65
2 -0.75
8
b的产生方法
第一行的b1=(a1-a2)/a2,其他类推
请问各位,如何做?

解决方案 »

  1.   

    create table t (a float);insert t select
    4.3 union all select 
    7 union all select 
    8 union all select 
    5.3 union all select 
    2 union all select 
    8 ;create  table test(id int not null auto_increment primary key ,a float);insert test(a) select * from t ;select a.a,cast((a.a-b.a)/b.a as decimal(18,2))as b
    from test a left join test b on a.id=b.id-1;/*
    +------+-------+
    | a    | b     |
    +------+-------+
    |  4.3 | -0.39 |
    |    7 | -0.13 |
    |    8 |  0.51 |
    |  5.3 |  1.65 |
    |    2 | -0.75 |
    |    8 |  NULL |
    +------+-------+
    */