Currently, I'm retrieving some data from a MySQL database table, but the problem is every time the query executed, by default MySQL sort the result according to automatic-generated-id which is field name [id]. I don't want to be sorted. In my case:
$keys = array(4, 1, 7, 8, 2, 5, 6);
$strKeys = implode(',', $keys);
$result = mysql_query('SELECT * FROM tableName WHERE id in('.$strKeys.')');
I want the result in order from what the $keys looks like: 4, 1, 7, 8, 2, 5, 6 BUT indeed, the result comes out in sorted order by [id]: 1, 2, 4, 5, 6, 7, 8
How to get the result and display as in order from the $keys.
You can try this.
SELECT * FROM tableName WHERE id in (4, 1, 7, 8, 2, 5, 6) order by find_in_set(id, '4, 1, 7, 8, 2, 5, 6');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With