3

SQL:

SELECT question,alt_1,alt_2,alt_3,alt_4 FROM questions WHERE id IN ('12','2','32','23')

PHP:

while ($questions = mysql_fetch_array($game)){
    echo "$questions[question]";
}

I want the rows to be printed in the order the values are sent to sql: 12 - 2 - 32 - 23.

But fetch_array/sql(?) seems to sort the values so the order becomes the following: 2 - 12 - 23 - 32 (starts with the lowest number..)

Is there any way to stop the array from being sorted by numbers!?

Kerem
  • 11,377
  • 5
  • 59
  • 58
user1671375
  • 269
  • 1
  • 3
  • 10
  • 1
    I think for your case UNION is the solution, because 12-2-32-23 are not relational (not logical to conclude any order by) –  Feb 02 '13 at 09:45
  • 1
    Solution posted here: http://stackoverflow.com/questions/1332434/mysql-order-by-in –  Feb 02 '13 at 09:48
  • 1
    Possible duplicate: [MySQL - ORDER BY values within IN()](http://stackoverflow.com/q/958627/1456376) – insertusernamehere Feb 02 '13 at 09:48
  • Thanks guys, the FILED function solved it. – user1671375 Feb 02 '13 at 09:51
  • @user1671375: Please do consider _not_ using the `mysql_*` extension anymore. It's being deprecated, because it's hopelessly out-dated. Use either the `mysqli_*` extension (`i` is for _improved_), or the more commonly used `PDO` extension – Elias Van Ootegem Feb 02 '13 at 10:03

2 Answers2

0

Try yhis,
" ORDER BY FIELD(id'12','2','32','23')"

          SQL: "SELECT question,alt_1,alt_2,alt_3,alt_4 FROM questions WHERE id IN 
          ('12','2','32','23') ORDER BY FIELD(id'12','2','32','23')"

           while($questions = mysql_fetch_array($game)){
           echo "$questions[question]";
       }
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
0

USE FIND_IN_SET

SELECT question,alt_1,alt_2,alt_3,alt_4 
FROM questions 
WHERE id IN ('12','2','32','23') 
ORDER BY FIND_IN_SET(id, '12,2,32,23')

Note FIND_IN_SET(id, '12,2,32,23') 12,2 without space

FIND_IN_SET(field, 'val1,val2')