-2

I have the following query

SELECT * FROM campaigns where campaign_id IN ( 'idStrOne', 'idStrTwo', 'idStrThree' );

The results of which are being ordered by the primary key of the table campaigns which is 'id'. This is not the order I want.

I want the results to come back in the same order as the arguments to the IN function. So in this case the order I want is

idStrOne, idStrTwo, idStrThree

How do I get this order?

Qantas 94 Heavy
  • 15,410
  • 31
  • 63
  • 82
32423hjh32423
  • 2,996
  • 7
  • 41
  • 56

1 Answers1

2

You could try adding a CASE expression in the ORDER BY Clause

SELECT * 
FROM campaigns 
WHERE campaign_id IN ( 'idStrOne', 'idStrTwo', 'idStrThree' )
ORDER BY 
(CASE campaign_id WHEN 'idStrOne' THEN 1 WHEN 'idStrTwo' THEN 2 ELSE 3 END);
Tim C
  • 69,130
  • 14
  • 71
  • 92