表名 table 
 n  v
 1  10
 3  50
 6  100
 4  60
 8  160求出 相邻两条n列 中 对应 v 相减的值
效果  3-1 40
     4-3 10
     6-4 40
     8-6 60
mysql 中 sql 语句实现,往指教!

解决方案 »

  1.   

    供參考。
    create table aa(n int,v int)
    insert into aa select 1,10
    union all select 3,50
    union all select 6,100
    union all select 4,60
    union all select 8,160
    select convert(varchar(100),a.n)+'-'+convert(varchar(100),(select max(n) from aa as c where n<a.n))n,
    (a.v-(select b.v from aa as b where b.n=(select max(n) from aa where n<a.n)))v
    from aa as a 
    where a.n<>1
    order by a.n