Limit results to last 10

后端 未结 4 508
执念已碎
执念已碎 2021-01-19 10:19

How can I display only the LAST 10 results from a mysql query?

I want anything prior to the last 10 results to be ignored when the results are output.



        
相关标签:
4条回答
  • 2021-01-19 11:15
    SELECT * FROM (SELECT * FROM graphs WHERE sid=2 ORDER BY id DESC LIMIT 10) g ORDER BY g.id
    

    Fetching last 10 records but resultset still in asc order.

    0 讨论(0)
  • 2021-01-19 11:16

    Order descending:

    ORDER BY id DESC LIMIT 10
    
    0 讨论(0)
  • 2021-01-19 11:20
    ORDER BY id DESC LIMIT 10
    

    at the end of your query. It will order your ids descending and therefore the first result will be the newest

    0 讨论(0)
  • 2021-01-19 11:22
    SELECT * FROM graphs WHERE sid=2 ORDER BY id DESC LIMIT 10;
    

    Explanation: by default results in an ORDER BY are sorted by ascending (ORDER BY ASC). By specifying descending (DESC) they are sorted in reverse order, the LIMIT 10 then takes the top 10 results.

    0 讨论(0)
提交回复
热议问题