0

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]

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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