List:General Discussion« Previous MessageNext Message »
From:Morten Date:November 30 2008 10:45pm
Subject:Re: Result ordering
View as plain text  
On Nov 30, 2008, at 11:42 PM, Andy Shellam wrote:

> Hi Morten,
>
> I think this is valid in MySQL (it certainly is for SQL Server) but  
> you can use a CASE statement directly in the ORDER BY clause.  Try  
> something like this:
>
> SELECT name
> FROM   foo
> WHERE  bar = 34
> OR     baz > 100
> ORDER BY CASE bar WHEN 34 THEN 0 ELSE 1 END ASC, baz DESC
> LIMIT 5;

Indeed! Thanks! I also found that I can simplify it a little by  
replacing CASE .. END with IF(condition, 0, 1).

Morten


Thread
Result orderingMorten30 Nov
  • Re: Result orderingAndy Shellam30 Nov
    • Re: Result orderingMorten30 Nov
  • RE: Result orderingMartin Gainty1 Dec