View Single Post

   
  #3 (permalink)  
Old 02-28-2008, 09:30 AM
Radoulov, Dimitre
 
Posts: n/a
Default Re: Getting back set order from the IN param


>> SELECT STRINGTEXT, TOKENID
>> FROM WEBSTRINGS
>> WHERE TOKENID IN (6,20,234,19,32,4,800,177)
>>
>> All I want is my resultset to come back in the order that I have defined
>> in
>> the IN clause, but unfortunately SQL is trying to be too helpful and
>> sorts
>> the numbers in the IN clause so that the resultset comes back with a
>> TOKENID
>> order of 4,6,19,20,32,177,234,800.
>>
>> I don't want this bloody order I want 6,20,234,19,32,4,800,177!!

>
> Far from elegant:
>
> SELECT STRINGTEXT, TOKENID
> FROM WEBSTRINGS
> WHERE TOKENID IN (6,20,234,19,32,4,800,177)
> ORDER BY
> TOKENID=6 DESC,
> TOKENID=20 DESC,
> TOKENID=234 DESC

....
[...]

This one is better

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
WHERE TOKENID IN (6,20,234,19,32,4,800,177)
ORDER BY
FIELD(TOKENID,6,20,234,19,32,4,800,177);


Regards
Dimitre





Reply With Quote