472,142 Members | 1,031 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

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:

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

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

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

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
"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.

Similar topics

3 posts views Thread by Bob Bedford | last post: by
4 posts views Thread by headware | last post: by
1 post views Thread by ralf321 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.