0

I am querying my table with a list of names.

SELECT * FROM mytable WHERE personName IN ('John','Andrew')

This returns 2 rows. In the table Andrew is before John.

  1. Andrew
  2. John

I want to order the response in the same order as in the WHERE personName IN ('John','Andrew')

codeNinja
  • 1,442
  • 3
  • 25
  • 61
  • You can never rely on selecting and ordering based on something hardcoded. Add a field that defines the order that you want, and order by that. – durbnpoisn Sep 11 '14 at 15:41
  • @durbnpoisn I think the point of this is that the order comes from user input, not static information in the table. – Barmar Sep 11 '14 at 15:44
  • Could be. This is why I left this as a comment. I'm simply suggesting that it's the wrong way to do it. It should still be using a unique identifier. – durbnpoisn Sep 11 '14 at 15:46

1 Answers1

0

SELECT * FROM mytable WHERE personName IN ('John','Andrew') ORDER BY FIELD('John','Andrew');

This is a duplicate of Sort by order of values in a select statement "in" clause in mysql by the way.

Community
  • 1
  • 1
Kell
  • 3,252
  • 20
  • 19
  • If it's just a duplicate, flag it rather than posting an answer with a link to the duplicate. BTW, you're missing the first argument to `FIELD()`. – Barmar Sep 11 '14 at 15:43