3

I use this query to show results from MySQL:

SELECT * FROM `table`
ORDER BY id ASC

But I want to show only the last 100 rows. How can I make it?

I tried this:

SELECT * FROM `table`
ORDER BY id ASC LIMIT 100

But it shows the first 100 rows, I need the last 100 rows...

Can you help me with this?

jarlh
  • 42,561
  • 8
  • 45
  • 63
tyrlaka
  • 154
  • 1
  • 5
  • 13
  • [Please, don't use mysql_* functions in new code.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) They are no longer maintained and are officially deprecated. Use mysqli or PDO – PHPhil Jun 21 '15 at 09:45

2 Answers2

6

You can do it with a sub-query:

SELECT * FROM (
    SELECT * FROM table ORDER BY id DESC LIMIT 100
) sub
ORDER BY id ASC

This will select the last 100 rows from table, and then order them in ascending order.

Nico Weisenauer
  • 284
  • 1
  • 7
2

Replace order by id asc with order by id desc to change the sorting order from ascending to descending and get last 100 rows.

potashin
  • 44,205
  • 11
  • 83
  • 107
  • I try with order by DESC, but the problem is that now they show me from top to the bottom, if make them with DESC, it show me from bottom to the top... i don't want like this... – tyrlaka Jun 21 '15 at 11:38
  • @tyrlaka: then try with Nico's solution. – potashin Jun 21 '15 at 11:39