* Yoed
> Lets say I have a query similar to:
>
> SELECT * FROM company WHERE id='3' OR id='1' OR id='7' ;
>
> I want to be able to get the results to come out ordered by the order
> the ids were searched (the sequence in the WHERE OR; 3 first then 1,
> then 7...), so the results would be like:
>
> Row   | id    | name
> -------------------
> 0     | 3     | Joe's Co
> 1     | 1     | Buymart
> 2     | 7     | The big evil
>
> As it is the results returns it like id 1, then 3, and 7.

Try using the FIELD() function (PHP example):

$idlist = "3,1,7";
mysql_query("SELECT * FROM company
  WHERE id IN ($id_list)
  ORDER BY FIELD(id,$id_list)");

... which expands to:

SELECT * FROM company
  WHERE id IN (3,1,7)
  ORDER BY FIELD(id,3,1,7);

The FIELD() function is documented here:

<URL: http://www.mysql.com/doc/en/String_functions.html >

I guess your 'id' field is an integer, and this is a string function, but it
should work anyway. If your 'id' is a string type column, you must quote
each id like you did in your example.

--
Roger


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to