David19801
David19801

Reputation: 11448

mysql last 5 in reverse order?

I have a mysql query:

SELECT `name` FROM `table1` WHERE id='0' ORDER BY `id` DESC LIMIT 5

This is ok, it gets me the last 5 elements with the very last in the table being the first in the returned array. ok.

But! I am trying to get the last 5 in normal table order (so the last in the main table would be number 5 in the returned array and the one before that would be 4 etc.)

I tried ASC, it didn't work...

How can I do this?

Upvotes: 0

Views: 3834

Answers (3)

Adeola
Adeola

Reputation: 1

For simplicity: use the dot I.e table1.name, e.g.

$query = "SELECT * FROM `users` ORDER BY `users`.`date_of_rsvp` DESC";

I think this should work.

Upvotes: 0

dugokontov
dugokontov

Reputation: 4612

Try this

select `name`
from (
   select `id`, `name`
   from `table1`
   where id='0'
   order by `id` desc
   limit 5
) as source
order by `id` asc

Upvotes: 3

Gumbo
Gumbo

Reputation: 655219

Make your query a subquery:

SELECT `name`
FROM (SELECT `id`, `name` FROM `table1` WHERE id='0' ORDER BY `id` DESC LIMIT 5) tmp
ORDER BY `id` ASC

Upvotes: 5

Related Questions