我想知道这是否有可能完全用MySQL完成。

在同一个数据点的2个不同条目之间,按最高级排序的前5个列表。

id     | year     | amount
-------+----------+--------
1      | 2016     | 4000
1      | 2017     | 3000
2      | 2016     | 12000
2      | 2017     | 15000
3      | 2016     | 100
3      | 2017     | 200


我基本上需要它按此顺序返回23,因为23有更多的改进。

1不应成为结果,因为几年之间并没有改善。

最佳答案

在这里检查:http://rextester.com/TBX28881

select      f1.id, (f2.amount - f1.amount) balance
from        foot f1
inner join  foot f2
on          f2.id = f1.id
and         f2.year = 2017
where       f1.year = 2016
order by    balance desc
limit       5;
;

id | balance
---- --------
 2    3000
 3     100
 1   -1000

关于mysql - 通过行之间的差异获取前5个结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42682220/

10-11 03:09
查看更多