MySQL Forums
Forum List  »  Newbie

Re: No Sort Order?
Posted by: Felix Geerinckx
Date: May 25, 2005 02:41AM

Fraser Murrell wrote:

> Felix - Thank you, not entirely sure what it means, but it works great!

The query was:

SELECT category, title, pubdate
FROM ds
WHERE category IN (2, 5, 4, 101, 7)
ORDER BY CASE category
WHEN 3 THEN 10
WHEN 5 THEN 20
WHEN 4 THEN 30
WHEN 101 THEN 40
WHEN 7 THEN 50
ELSE 999
END


An 'ORDER BY something' clause sorts your records according to the value of 'something' for each record (standard from low to high).

In this case, something is a CASE ... END construct
(see http://dev.mysql.com/doc/mysql/en/case-statement.html)

This CASE ... END construct translates your category values into numbers (the 10 ... 50, 999) that provide the correct numbers for the wanted sorting.

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

Options: ReplyQuote


Subject
Written By
Posted
May 24, 2005 02:48PM
May 24, 2005 03:24PM
May 25, 2005 01:56AM
Re: No Sort Order?
May 25, 2005 02:41AM


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.