Unix Technical Forum

Getting back set order from the IN param

This is a discussion on Getting back set order from the IN param within the MySQL forums, part of the Database Server Software category; --> 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 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:30 AM
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




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (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
TOKENID=19 DESC,
TOKENID=32 DESC,
TOKENID=4 DESC,
TOKENID=800 DESC,
TOKENID=177 DESC;


Regards
Dimitre


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #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





Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #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/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:30 AM
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 09:30 AM
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






Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 09:30 AM
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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 09:30 AM
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:26 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com