...

View Full Version : Ordering in queries by sequence of elements in "IN"



alex375
11-01-2007, 01:08 PM
Hello All,

I need a query which delivers results in the order I have in the element sequence after "IN".


SELECT aid, cid, vid FROM table WHERE aid IN (5,7,3)

I want to see the resuts of the query submitted in the same order, it means the first row should be with aid 5, the second with aid 7 and the third with 3.

Without "ORDER BY" I get the results in sequence 3, 5, 7.

Please help

guelphdad
11-01-2007, 02:21 PM
Two ways to do this one proprietary, one standard sql.

This one can be done in mysql only and isn't portable to other applications


ORDER BY FIELD(fieldname,5,7,3)

and this one is portable


ORDER BY
CASE
WHEN fieldname = 5 then 0
WHEN fieldname = 7 then 1
WHEN fieldname = 3 then 2
END



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum