Click here to get back home

Select last 3 items in ascending order

 HomeNewsGroups | Search | About
 mailing.database.mysql    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Select last 3 items in ascending order Nick Weisser 08-02-2006
Get Chitika Premium
Posted by Nick Weisser on August 2, 2006, 8:42 am
Please log in for more thread options
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

Posted by strawberry on August 2, 2006, 8:57 am
Please log in for more thread options

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


Posted by Nick Weisser on August 2, 2006, 9:06 am
Please log in for more thread options
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!



Posted by Thomas Bartkus on August 2, 2006, 10:01 am
Please log in for more thread options
> 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





Posted by strawberry on August 2, 2006, 11:34 am
Please log in for more thread options

Thomas Bartkus 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.
> >
>
> 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;


Similar ThreadsPosted
ORDER BY clause fails to do descending order on this field column February 8, 2006, 7:48 pm
How to get the number of each unique items? October 28, 2005, 7:43 pm
Count Number of Items in each Category April 9, 2006, 11:47 pm
MySQL, SELECTing items with GROUP BY, HAVING COUNT(*) > 1 October 9, 2006, 10:16 pm
Help with counting total items using hierarchical data structure August 1, 2007, 1:27 am
update [table] select - but I only need part of the column field value to select January 26, 2006, 2:38 pm
Query help: Get category and number of items in one query June 25, 2005, 1:36 am
Why does 'SELECT * FROM countries' truncate characters and 'SELECT country FROM countries' does not ? December 7, 2005, 5:42 am
need help - select from one table where desn't exists in (select from another table where status=1) September 20, 2006, 8:34 pm
Order by str to int May 12, 2006, 7:42 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap