Take the 2-minute tour ×
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.

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

1 Answer 1

up vote 1 down vote accepted

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

SELECT 
  PK_ONE 
from 
  TABLE_A 
where 
  PK_ONE in (2,88,3999,4,282,33399,1,394)
ORDER BY
  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

 
discard

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.