开发者

Best way to fetch last 4 rows from a result set using mysql

开发者 https://www.devze.com 2023-01-02 22:16 出处:网络
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.

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.

but, i开发者_JAVA技巧 need the records to be fetch from last4...i.e,

Record-3
Record-4
Record-5
Record-6


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.


SELECT * FROM tablename ORDER BY id DESC LIMIT 0,4 

will give you the last 4 records ("last" when you order the table by id which is supposed to be an auto-increment field here.)


If you how that there is always 6 rows you could use limit.

SELECT * FROM Tabel LIMIT 2, 4
0

精彩评论

暂无评论...
验证码 换一张
取 消