What's the most efficient way to select the last n rows in a table without changing the table's structure?

后端 未结 8 1858
天命终不由人
天命终不由人 2020-12-10 02:25

What\'s the most efficient way to select the last n number of rows in a table using mySQL? The table contains millions of rows, and at any given time I don\'t know how large

8条回答
  •  遥遥无期
    2020-12-10 02:47

    Actually the right way to get last n rows in order is to use a subquery:

    (SELECT id, title, description FROM my_table ORDER BY id DESC LIMIT 5) 
    ORDER BY tbl.id ASC
    

    As this way is the only I know that will return them in right order. The accepted answer is actually a solution for "Select first 5 rows from a set ordered by descending ID", but that is most probably what you need.

提交回复
热议问题