List:General Discussion« Previous MessageNext Message »
From:Joeri De Backer Date:November 9 2010 9:15am
Subject:Re: Order by "in" clause
View as plain text  
On Tue, Nov 9, 2010 at 10:09 AM, Mark Goodge <mark@stripped> wrote:
> Hi,
>
> I have a query like this:
>
> select id, title from product where id in (1,3,5,8,10)
>
> What I want it to do is return the rows in the order specified in the "in"
> clause, so that this:
>
> select * from product where id in (10,3,8,5,1)
>
> will give me results in this order:
>
> +------+---------+
> | id   | title   |
> +------+---------+
> |  10  |    foo  |
> +------+---------+
> |   3  |    baz  |
> +------+---------+
> |   8  |    bar  |
> +------+---------+
> |   5  | wibble  |
> +------+---------+
> |   1  | flirble |
> +------+---------+
>
> Is this possible? If so, how?
>

select * from product where id in (10,3,8,5,1) order by field(id,10,3,8,5,1)

should do the trick...

Regards,

Joeri
Thread
Order by "in" clauseMark Goodge9 Nov
  • Re: Order by "in" clauseJoeri De Backer9 Nov
    • RE: Order by "in" clauseDaevid Vincent9 Nov