By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,773 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,773 IT Pros & Developers. It's quick & easy.

Select last 3 items in ascending order

P: n/a
Hi there,

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

This does the trick in descending order:

select * from user_menu_main
where deleted = 0 and hidden = 0
order by date desc
limit 3

Your comments would be very much appreciated.

Cheers,
Nick
Aug 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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:

select * from user_menu_main
where deleted = 0 and hidden = 0
order by date desc
limit 3

Your comments would be very much appreciated.

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

Aug 2 '06 #2

P: n/a
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

P: n/a
"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:

select * from user_menu_main
where deleted = 0 and hidden = 0
order by date desc
limit 3

Your comments would be very much appreciated.
That problem is a natural for a temporary table.

create temporary table tmp
select * from user_menu_main
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

P: n/a

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:

select * from user_menu_main
where deleted = 0 and hidden = 0
order by date desc
limit 3

Your comments would be very much appreciated.

That problem is a natural for a temporary table.

create temporary table tmp
select * from user_menu_main
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 (
select * from user_menu_main
where deleted = 0 and hidden = 0
order by date desc
limit 3) tmp
order by date asc;

Aug 2 '06 #5

P: n/a
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>
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:
>
select * from user_menu_main
where deleted = 0 and hidden = 0
order by date desc
limit 3
>
Your comments would be very much appreciated.
>
That problem is a natural for a temporary table.

create temporary table tmp
select * from user_menu_main
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.