View Single Post

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

"Yobbo" <info@SpamMeNot.co.uk> wrote:

> My query is as follows:
>
> 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,


SQL does not guarantee a certain order of the result set unless you
explicitly ordered one via an ORDER BY clause. If you want a certain
order, you have to specify it via ORDER BY.

> 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.


This is a mere coincidence. In fact MySQL sorts the values in the IN
clause in order to be able to do an efficient search on the index.
Therefor you get your result in index order - which is ascending for
most storage engines. If your query hits a MERGE or cluster table the
result order would be data dependent or completely random.


XL
--
Axel Schwenke, Senior Software Developer, MySQL AB

Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/
Reply With Quote