1

Is there a way I can return a set of mySQL rows in the order they're called. For instance, when I call this:

SELECT * FROM heroes WHERE id IN ( 41 , 48 , 38 , 14 , 47 , 44 ) LIMIT 6 

I'd like the rows returned in that order. Is there a php function I could apply to the results afterward to achieve this ordering?

bjork24
  • 3,153
  • 7
  • 35
  • 46
  • 4
    Exact duplicate: http://stackoverflow.com/questions/396748/ordering-by-the-order-of-values-in-a-sql-in-clause – ChssPly76 Dec 23 '09 at 20:46
  • 1
    Possible duplicate of [Ordering by the order of values in a SQL IN() clause](https://stackoverflow.com/questions/396748/ordering-by-the-order-of-values-in-a-sql-in-clause) – Machavity Jan 30 '19 at 01:00

2 Answers2

0

You could try ORDER BY FIELD(id, 41, 48, 38, 14, 47, 44) ASC.

EDIT: Found a better function than FIND_IN_SET.

Matthew
  • 2,024
  • 15
  • 19
0

Yes, take a look at "Ordering by the order of values in a SQL IN() clause" answers. The solution is very well described there.

Community
  • 1
  • 1
Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82