Select last 3 items in ascending order
Select last 3 items in ascending order
am 02.08.2006 14:42:08 von Nick Weisser
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
Re: Select last 3 items in ascending order
am 02.08.2006 14:57:13 von zac.carey
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
Re: Select last 3 items in ascending order
am 02.08.2006 15:06:44 von Nick Weisser
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!
Re: Select last 3 items in ascending order
am 02.08.2006 16:01:57 von Thomas Bartkus
"Nick Weisser" wrote in message
news:44d09da3$1_1@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
Re: Select last 3 items in ascending order
am 02.08.2006 17:34:51 von zac.carey
Thomas Bartkus wrote:
> "Nick Weisser" wrote in message
> news:44d09da3$1_1@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;
Re: Select last 3 items in ascending order
am 02.08.2006 21:36:29 von Thomas Bartkus
"strawberry" wrote in message
news:1154532891.787496.157610@i3g2000cwc.googlegroups.com...
>
> Thomas Bartkus wrote:
> > "Nick Weisser" wrote in message
> > news:44d09da3$1_1@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