need to make table with asc and desc columns like below from length column.
+----------+-------+-------+-------+
| length | ordNr | asc | desc |
+----------+-------+-------+-------+
| 11 | 0 | 11 | 119 |
| 99 | 1 | 110 | 108 |
| 5 | 2 | 115 | 9 |
| 4 | 3 | 119 | 4 |
+----------+-------+-------+-------+
可以在SQL中实现吗?我知道如何在PHP中做到这一点,但也许javascript解决方案更容易? 我就是这样做的in jsfiddle
答案 0 :(得分:0)
我无法看到如何在SQL中执行此操作有两个原因
在编程语言中将'length'列读入数组并处理该数组中的所有必要计算会更容易。
答案 1 :(得分:0)
尝试:
select max(t1._length) _length, t1._ordNr,
sum(case when t1._ordNr >=t2._ordNr then t2._length end) _asc,
sum(case when t1._ordNr <=t2._ordNr then t2._length end) _desc
from myTable t1 cross join myTable t2
group by t1._ordNr
(下划线添加到列名中以避免与SQL保留字冲突。)