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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| > 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 |
| |||
| >> 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 |
| |||
| "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/ |
| |||
| 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. |
| |||
| 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 |
| |||
| 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. |
| ||||
| 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. |