使用mysql从结果集中获取最后4行的最佳方法 [英] Best way to fetch last 4 rows from a result set using mysql
本文介绍了使用mysql从结果集中获取最后4行的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我,我需要使用 mysql 从结果集中获取最后 4 行.结果集总共返回6条记录.
Can any one please let me know, that, i need to fetch last 4 rows from a result-set using mysql. The result-set returns totally 6 records.
但是,我需要从 last4 中获取记录......即,
but, i need the records to be fetch from last4...i.e,
Record-3
Record-4
Record-5
Record-6
推荐答案
要获取最后的 x 行数,但让它们按升序返回,请使用:
To get the last x number of rows, but have them returned in ascending order, use:
SELECT x.value
FROM (SELECT y.value
FROM TABLE y
ORDER BY y.value DESC
LIMIT 4) x
ORDER BY x.value
答案要求您根据所需的行创建派生表(也称为内联视图).然后外部查询重新排列显示值.
The answer requires that you create a derived table (AKA inline view) based on the rows you want. Then the outer query re-orders the values for presentation.
这篇关于使用mysql从结果集中获取最后4行的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文