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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-21-04, 05:56
WhSox21 WhSox21 is offline
Registered User
 
Join Date: Dec 2003
Posts: 31
Select ordering...

I have a query which can be big or small. Basically what it does is something like this...
select * from table where a = 3 or a = 2 or a = 4

The problem I'm having is when I'm fetching the array it doesn't return in the same order as I searched for it. So in this example I want the results returned in 3,2,4 not 2,3,4 like it does. Is there any way to fix this?
__________________
vBulletin Hacking God - Portfolio
FireFoxForum.com - Where people talk about FireFox.
Reply With Quote
  #2 (permalink)  
Old 12-21-04, 08:12
matt_p matt_p is offline
Registered User
 
Join Date: Dec 2004
Location: Europe
Posts: 20
SQL Results have no "defined order" as long as you dont' put an "order by" clause in the select, which will not help in your case if I understand it correctly.

An "unsure" workaround may be the following

select * from table where a=3
union all
select * from table where a=2
union all
select * from table where a=4.

(depens on the RDBMS+Version which order your get).

The real good (?) solution would be:

create table "table2" (ordering int, searcharg int)
insert into table2 values (1, 3)
insert into table2 values (2, 2)
insert into table2 values (3, 4)


select table.* from table , table2
where table.a = table2.searcharg
and table.a in (3,2,4)
order by table2.ordering

(I've put als the "or"-predicates in an "in" -predicate, which normally performs better).
Reply With Quote
  #3 (permalink)  
Old 12-21-04, 09:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
mysql has a really neat function you can use for this custom ordering

SELECT foo, bar
FROM yourtable
WHERE a IN (3,2,4)
ORDER BY FIELD(a,3,2,4)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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