-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,750
  • 31
  • 68
  • 83
32423hjh32423
  • 3,048
  • 7
  • 44
  • 59

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
  • 70,053
  • 14
  • 74
  • 93