472,142 Members | 1,031 Online

# Select last 3 items in ascending order

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
Aug 2 '06 #1
5 9855

Nick Weisser wrote:
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
i'm guessing that 'desc' bit's got something to do with it

Aug 2 '06 #2
strawberry wrote:
i'm guessing that 'desc' bit's got something to do with it
But this will not select the 3 last items in ascending order, but the
first 3!
Aug 2 '06 #3
"Nick Weisser" <ni**@nospam.freemails.chwrote in message
news:44**********@news.bluewin.ch...
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

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

Aug 2 '06 #4

Thomas Bartkus wrote:
"Nick Weisser" <ni**@nospam.freemails.chwrote in message
news:44**********@news.bluewin.ch...
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

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
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;

Aug 2 '06 #5
"strawberry" <za*******@gmail.comwrote in message
>
Thomas Bartkus wrote:
"Nick Weisser" <ni**@nospam.freemails.chwrote in message
news:44**********@news.bluewin.ch...
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
>
>
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

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:
... 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
Aug 2 '06 #6

### This discussion thread is closed

Replies have been disabled for this discussion.