MySQL Forums
Forum List  »  Newbie

Re: Selecting and sorting the last 5 entries.
Posted by: Felix Geerinckx
Date: August 16, 2005 02:34AM

Dimiter Ivanov wrote:

> "SELECT * FROM songs WHERE 1 ORDER BY ID DESC
> LIMIT 5"
>
> But what if i want to sort those last 5 entries by song title ?
> I was able to do that with subqueries, but on my current server i have older version of mysql that
> does not support subqueries.
>
> Any ideas ?

The following kludge seems to work (using a UNION):

(SELECT * FROM songs ORDER BY ID DESC LIMIT 5)
UNION
(SELECT * FROM songs ORDER BY ID DESC LIMIT 5)
ORDER BY TITLE LIMIT 5;

Depending on how well you know your data, you could even replace one of the SELECTs in your UNION with

(SELECT 0, 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
Re: Selecting and sorting the last 5 entries.
August 16, 2005 02:34AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.