Bluehost.com Web Hosting $6.95

Getting back set order from the IN param

This is a discussion on Getting back set order from the IN param within the MySQL Database forums, part of the Database Forums category; Hi All My query is as follows: SELECT STRINGTEXT, TOKENID FROM WEBSTRINGS WHERE TOKENID IN (6,20,234,19,32,...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-05-2006
Yobbo
 
Posts: n/a
Default Getting back set order from the IN param

Hi All

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, 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!!

Sorry for my rant, but its got my hot under the collar.

Is there anyway round this?

Thanks

Yobbo




Reply With Quote
  #2 (permalink)  
Old 11-05-2006
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
TOKENID=19 DESC,
TOKENID=32 DESC,
TOKENID=4 DESC,
TOKENID=800 DESC,
TOKENID=177 DESC;


Regards
Dimitre


Reply With Quote
  #3 (permalink)  
Old 11-05-2006
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
  #4 (permalink)  
Old 11-05-2006
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
  #5 (permalink)  
Old 11-05-2006
Yobbo
 
Posts: n/a
Default Re: Getting back set order from the IN param

Many thanks Dimitre

This looks like a winner will let you know how I get on.

Rgds Yobbo

"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message
news:454dcfd6$0$49200$14726298@news.sunsite.dk...

>> 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
  #6 (permalink)  
Old 11-06-2006
Bill Karwin
 
Posts: n/a
Default Re: Getting back set order from the IN param

Yobbo wrote:
> 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


See the docs for FIND_IN_SET() here:
http://dev.mysql.com/doc/refman/5.0/...functions.html

For example:

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

Note the quotes: IN() has variable arguments which are a
comma-separated list of integers, but FIND_IN_SET() has two arguments,
the latter of which is a quoted string.

Regards,
Bill K.
Reply With Quote
  #7 (permalink)  
Old 11-06-2006
Yobbo
 
Posts: n/a
Default Re: Getting back set order from the IN param

Hi Bill

Many thanks for this.

Do you know if your method is more efficient than Dimitre's
FIELD(TOKENID,6,20,234,19,32,4,800,177) method??

Rgds Yobbo



"Bill Karwin" <bill@karwin.com> wrote in message
news:eimeo201jf8@enews3.newsguy.com...
Yobbo wrote:
> 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


See the docs for FIND_IN_SET() here:
http://dev.mysql.com/doc/refman/5.0/...functions.html

For example:

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

Note the quotes: IN() has variable arguments which are a
comma-separated list of integers, but FIND_IN_SET() has two arguments,
the latter of which is a quoted string.

Regards,
Bill K.


Reply With Quote
  #8 (permalink)  
Old 11-07-2006
Bill Karwin
 
Posts: n/a
Default Re: Getting back set order from the IN param

Yobbo wrote:
> Do you know if your method is more efficient than Dimitre's
> FIELD(TOKENID,6,20,234,19,32,4,800,177) method??



I don't know for sure. It may depend partly on your indexes, data
distribution, etc. One way to know for sure is for you to try both
methods under some benchmarking tool (e.g.
http://xaprb.com/mysql-query-profiler/).

Regards,
Bill K.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:11 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0