Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't want MySQL to sort the results found

Tags:

php

mysql

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.

like image 952
Thavarith Avatar asked Mar 29 '12 04:03

Thavarith


1 Answers

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');
like image 158
xda1001 Avatar answered Oct 15 '22 04:10

xda1001