If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query result order when using IN clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-23-05, 15:00
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Query result order when using IN clause

hi,

The following query returns a result where the order of the rows is not in the desired order 10,5,9 but 5,9,10. I want to order the returned rows based on how they are ordered in the IN clause.

SELECT * from test WHERE OrderID IN (10,5,9)

OrderID is a primary key in table test
Reply With Quote
  #2 (permalink)  
Old 11-23-05, 20:58
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
What you have is going to return anything with an order id in that group ordered by the order they are found. (That makes sense, right?.) If you want to explicitly set an order, you should use the ORDER BY clause, however I don't think you can set a "custom" order for an ORDER BY clause. What you may have to do is do a UNION ALL.

Code:
(SELECT * FROM test WHERE orderID = 10)
UNION ALL
(SELECT * FROM test WHERE orderID = 5)
UNION ALL
(SELECT * FROM test WHERE orderID = 9)
If that won't work for your situation (perhaps you have many many orderIDs), the only other thing I can think of right now is to make a new table with your orderids and assign each an "order number" to specify the order in which they should appear. Then join on that table and sort by the order number.
Reply With Quote
  #3 (permalink)  
Old 11-23-05, 23:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by jfulton
however I don't think you can set a "custom" order for an ORDER BY clause.
yes, you can
Code:
SELECT * from test WHERE OrderID IN (10,5,9)
order by field(OrderID,10,5,9)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 11-24-05, 07:18
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
Ahhh, I stand corrected. There's another new trick up the sleeve. Maybe I should stick to the php forums .

Nice to have you back though Rudy.
Reply With Quote
  #5 (permalink)  
Old 11-24-05, 07:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by jfulton
Nice to have you back though Rudy.
thank you, i appreciate it, although i am not really back, i was just incredibly bored last night
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-24-05, 08:14
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Smile

Awesome
thanks to both of you
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On