# Select last 3 items in ascending order

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi there,

I'm not sure how to select the last 3 items in ascending order.

This does the trick in descending order:

where deleted = 0 and hidden = 0
order by date desc
limit 3

Cheers,
Nick

## Re: Select last 3 items in ascending order

Nick Weisser wrote:

i'm guessing that 'desc' bit's got something to do with it

## Re: Select last 3 items in ascending order

strawberry wrote:

But this will not select the 3 last items in ascending order, but the
first 3!

## Re: Select last 3 items in ascending order

That problem is a natural for a temporary table.

create temporary table tmp
where deleted = 0 and hidden = 0
order by date desc
limit 3;

select * from tmp
order by date desc;

Thomas Bartkus

## Re: Select last 3 items in ascending order

Thomas Bartkus wrote:

I see, you want to limit your select to last three items of a list, but
have those items listed in the ordinary order. Well Thomas's solution
is probably the most straightfoward, although (depending on your
version) you don't actually need to create the temporary table:

(untested)

select * from (
where deleted = 0 and hidden = 0
order by date desc
limit 3) tmp
order by date asc;

## Re: Select last 3 items in ascending order

> ... you don't actually need to create the temporary table:

On the other hand - there ain't no reason not to ;-)

That subquery you show, for instance, uses a temporary table behind the
scenes.
Thomas Bartkus