tyrlaka
tyrlaka

Reputation: 164

How can I show last 100 rows?

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?

Upvotes: 3

Views: 16514

Answers (2)

Nico Weisenauer
Nico Weisenauer

Reputation: 284

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.

Upvotes: 6

potashin
potashin

Reputation: 44581

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

Upvotes: 2

Related Questions