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
Nick Weisser [ Mi, 02 August 2006 14:42 ] [ ID #1414392 ]

Re: Select last 3 items in ascending order

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
zac.carey [ Mi, 02 August 2006 14:57 ] [ ID #1414393 ]

Re: Select last 3 items in ascending order

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!
Nick Weisser [ Mi, 02 August 2006 15:06 ] [ ID #1414394 ]

Re: Select last 3 items in ascending order

"Nick Weisser" <nick [at] nospam.freemails.ch> wrote in message
news:44d09da3$1_1 [at] 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
Thomas Bartkus [ Mi, 02 August 2006 16:01 ] [ ID #1414395 ]

Re: Select last 3 items in ascending order

Thomas Bartkus wrote:
> "Nick Weisser" <nick [at] nospam.freemails.ch> wrote in message
> news:44d09da3$1_1 [at] 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;
zac.carey [ Mi, 02 August 2006 17:34 ] [ ID #1414397 ]

Re: Select last 3 items in ascending order

"strawberry" <zac.carey [at] gmail.com> wrote in message
news:1154532891.787496.157610 [at] i3g2000cwc.googlegroups.com...
>
> Thomas Bartkus wrote:
> > "Nick Weisser" <nick [at] nospam.freemails.ch> wrote in message
> > news:44d09da3$1_1 [at] 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
Thomas Bartkus [ Mi, 02 August 2006 21:36 ] [ ID #1414405 ]
Datenbanken » mailing.database.mysql » Select last 3 items in ascending order

Vorheriges Thema: List words in fulltext index
Nächstes Thema: Mysql Username limit