I have a query like this: SELECT * FROM table WHERE id IN (2,4,1,5,3);
However, when I print it out, it\'s automatically sorted 1,2,3,4,5. How can we ma
i ask this :
mysql order by issue
the answers that i get and all the credit belong to them is :
You can use a CASE operator to specify the order:
SELECT * FROM table
WHERE id IN (3,6,1,8,9)
ORDER BY CASE id WHEN 3 THEN 1
WHEN 6 THEN 2
WHEN 1 THEN 3
WHEN 8 THEN 4
WHEN 9 THEN 5
END
in php u can do it like :
$v){
$sql .= 'WHEN ' . $v . ' THEN ' . $k . "\n";
}
$sql .= 'END ';
echo $sql;
?>