sort asc w/ most recent

hi,

i want to display the five most recent rows in my table (there is a
timestamp), but sorted in ascending order.

the problem is that when i call

SELECT * FROM spam ORDER BY time ASC LIMIT 5

it shows the first five entries in ascending order, but i want the last
five.

needless to say, it works fine with descending order..

thanks.
mark [ So, 20 August 2006 11:01 ] [ ID #1437164 ]

Re: sort asc w/ most recent

Mark wrote:
> hi,
>
> i want to display the five most recent rows in my table (there is a
> timestamp), but sorted in ascending order.
>
> the problem is that when i call
>
> SELECT * FROM spam ORDER BY time ASC LIMIT 5
>
> it shows the first five entries in ascending order, but i want the last
> five.
>
> needless to say, it works fine with descending order..
>
> thanks.
Do you want first five entrys in descending order or last five entrys
in ascending order or what ????
Davie [ So, 20 August 2006 11:33 ] [ ID #1437165 ]

Re: sort asc w/ most recent

Mark wrote:
> hi,
>
> i want to display the five most recent rows in my table (there is a
> timestamp), but sorted in ascending order.
>
> the problem is that when i call
>
> SELECT * FROM spam ORDER BY time ASC LIMIT 5
>
> it shows the first five entries in ascending order, but i want the last
> five.
>
> needless to say, it works fine with descending order..
>
> thanks.

SELECT * FROM (SELECT * FROM spam ORDER BY time DESC LIMIT 5) t1 ORDER
BY time ASC;
zac.carey [ So, 20 August 2006 11:44 ] [ ID #1437166 ]

Re: sort asc w/ most recent

strawberry wrote:
> Mark wrote:
> > hi,
> >
> > i want to display the five most recent rows in my table (there is a
> > timestamp), but sorted in ascending order.
> >
> > the problem is that when i call
> >
> > SELECT * FROM spam ORDER BY time ASC LIMIT 5
> >
> > it shows the first five entries in ascending order, but i want the last
> > five.
> >
> > needless to say, it works fine with descending order..
> >
> > thanks.
>
> SELECT * FROM (SELECT * FROM spam ORDER BY time DESC LIMIT 5) t1 ORDER
> BY time ASC;

SELECT * FROM (SELECT * FROM spam LEFT JOIN users USING (user_id) ORDER
BY time DESC LIMIT $view) t1 ORDER BY time ASC

wow..it works. that's so ugly :p
thank you so much!
but what does this "t1" mean?
mark [ So, 20 August 2006 22:28 ] [ ID #1437172 ]

Re: sort asc w/ most recent

"Mark" <mnbayazit [at] gmail.com> wrote in message
news:1156105682.598578.131630 [at] b28g2000cwb.googlegroups.com.. .
>
> strawberry wrote:
>> Mark wrote:
>> > hi,
>> >
>> > i want to display the five most recent rows in my table (there is a
>> > timestamp), but sorted in ascending order.
>> >
>> > the problem is that when i call
>> >
>> > SELECT * FROM spam ORDER BY time ASC LIMIT 5
>> >
>> > it shows the first five entries in ascending order, but i want the last
>> > five.
>> >
>> > needless to say, it works fine with descending order..
>> >
>> > thanks.
>>
>> SELECT * FROM (SELECT * FROM spam ORDER BY time DESC LIMIT 5) t1 ORDER
>> BY time ASC;
>
> SELECT * FROM (SELECT * FROM spam LEFT JOIN users USING (user_id) ORDER
> BY time DESC LIMIT $view) t1 ORDER BY time ASC
>
> wow..it works. that's so ugly :p
> thank you so much!
> but what does this "t1" mean?

It's just aliasing the return set and is required for syntax correctness I
believe.
Jim Langston [ So, 20 August 2006 23:07 ] [ ID #1437173 ]

Re: sort asc w/ most recent

Jim Langston wrote:
>> but what does this "t1" mean?
>
> It's just aliasing the return set and is required for syntax correctness I
> believe.

Right. Technically, it's called a "correlation name". It's required
when you use a subquery as a derived table in the FROM clause, because
otherwise how would you refer to columns from that derived table?

SELECT foo FROM table1, (SELECT foo FROM table2);

Does the above retrieve foo from table1 or foo from table2? To resolve
the ambiguity, we use correlation names:

SELECT t1.foo FROM table1 AS t1, (SELECT foo FROM table2) AS t2

Regards,
Bill K.
Bill Karwin [ Mo, 21 August 2006 00:22 ] [ ID #1438186 ]

Re: sort asc w/ most recent

Bill Karwin wrote:
> Jim Langston wrote:
> >> but what does this "t1" mean?
> >
> > It's just aliasing the return set and is required for syntax correctness I
> > believe.
>
> Right. Technically, it's called a "correlation name". It's required
> when you use a subquery as a derived table in the FROM clause, because
> otherwise how would you refer to columns from that derived table?
>
> SELECT foo FROM table1, (SELECT foo FROM table2);
>
> Does the above retrieve foo from table1 or foo from table2? To resolve
> the ambiguity, we use correlation names:
>
> SELECT t1.foo FROM table1 AS t1, (SELECT foo FROM table2) AS t2
>
> Regards,
> Bill K.

i see. i thought it had to do with naming, but as far as i can see "t1"
was never referenced, so i was wondering why it was necessary. but i
guess for syntax's sake..
mark [ Mo, 21 August 2006 10:49 ] [ ID #1438191 ]
Datenbanken » mailing.database.mysql » sort asc w/ most recent

Vorheriges Thema: MySQL Performance Statistics
Nächstes Thema: join tables?