MySQL Forums
Forum List  »  Newbie

Re: Query result - not to be sorted asc or desc
Posted by: Jay Pipes
Date: July 24, 2005 09:18AM

Perhaps the simplest and most elegant solution would be to add a field to your locations table which is used for the ORDER BY expression. For instance:

ALTER TABLE locations ADD COLUMN order_by INT UNSIGNED NOT NULL;

Then, you could update this field for each of the records, like so:

UPDATE locations SET order_by = 1 WHERE location = 'd';
UPDATE locations SET order_by = 2 WHERE location = 'a';
UPDATE locations SET order_by = 3 WHERE location = 'c';
UPDATE locations SET order_by = 4 WHERE location = 'b';

Then, when doing SELECTs, simply ORDER BY this new field:

SELECT l.notes FROM locations l
WHERE l.location IN ('a','b','c','d')
ORDER BY order_by ASC;

Jay Pipes
Community Relations Manager, North America, MySQL Inc.

Got Cluster? http://www.mysql.com/cluster
Personal: http://jpipes.com

Options: ReplyQuote


Subject
Written By
Posted
Re: Query result - not to be sorted asc or desc
July 24, 2005 09:18AM


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.