Osaze Hezekiah
Osaze Hezekiah

Reputation: 11

How to order SELECT results based on the IN() list order?

I am having some troubles ordering item using MySQL, example

$USER_INPUT = '10,4';

$SQL = "SELECT * FROM table Where ID IN ($USER_INPUT)";

If I use this query, it will order the items EG: 4 and 10, instead of the user input 10 and 4.

Upvotes: 0

Views: 166

Answers (3)

Osaze Hezekiah
Osaze Hezekiah

Reputation: 11

I used

 SELECT A.ID, A.Name 
 FROM Properties A 
 WHERE A.ID IN (110,105,104,106)
 Order By case A.ID 
   when 110 then 0
   when 105 then 1
   when 104 then 2
   when 106 then 3 end

and it worked.

Upvotes: 0

Karthick
Karthick

Reputation: 381

you can create custom order by in order by like the below

ORDER BY FIELD(column_name,'1,5,6,7,10,43,867')

Upvotes: 1

Manoj AP
Manoj AP

Reputation: 1

May be your query returns in a descending manner try to place an order by clause at the end of the query ORDER BY id ASC

Upvotes: 0

Related Questions