Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm fetching data from a table "TABLE_A", it's having Primary Key as "PK_ONE" (which is unsigned INT) .

I'm fetching data from "TABLE_A" with where clause on PK_ONE ; Ex: SELECT PK_ONE from TABLE_A where PK_ONE in (2,88,3999,4,282,33399,1,394);

When I'm using dataset mentioned above in PHP cursor; I want to ensure that data fetched comes in the sequence of values of "PK_ONE" in WHERE clause.

Can you please give me some direction to achieve same? [Currently rows are coming in asending order]

share|improve this question
up vote 1 down vote accepted

You are looking for ORDER BY FIELD(), and it looks like this:

  PK_ONE in (2,88,3999,4,282,33399,1,394)
  FIELD (PK_ONE, 2,88,3999,4,282,33399,1,394)

FIELD() is a function which returns the position of an element (first argument) in a list of values (2nd, 3rd, ... arguments).

So you need to generate/template the list of values twice for this query.

share|improve this answer
Thanks Shlomi, PERFECT. This is what I was looking for. – Pawan Mude Oct 2 '13 at 5:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.