Mar 2, 2011

Order by field

Ordering by the order of values in a SQL IN() Clause, which can be done by ‘order by field(id, [ids from first])’.
For example,

$enames = array('a','b','d');

$sql = "SELECT name from employees 
          WHERE name in ('" . implode("','",$enames) . "') 
          ORDER BY FIELD(name, '" .implode("','",$enames). "')";
 

Nice! Ref – http://stackoverflow.com/questions/396748/ordering-by-the-order-of-values-in-a-sql-in-clause

 

Leave a comment