[thelist] Reordering result set (PHP/MySQL)

r937 rudy at r937.com
Mon Feb 4 09:42:37 CST 2008

>> order by field(id,3,7,2,1)
> Hey, ok, so MySQL wins.

if you would like to see the non-proprietary standard sql version, it goes 
like this:

   order by 
     case id
       when 3 then 1
       when 7 then 2
       when 2 then 3
       when 1 then 4
                else 0 end

the FIELD function is just shorthand for this CASE expression, which will 
work in all database management systems


More information about the thelist mailing list