3

I have a sql statement that brings back ids. Currently I am ordering the id's with the usual "ORDER BY id". What I need to be able to do is have the query order the first 3 rows by specific id's that I set. The order the remaining as it is currently. For example, I want to say the first 3 rows will be id's 7,10,3 in that order, then the rest of the rows will be ordered by the id as usual.

right now i just have a basic sql statement...

SELECT * from cards ORDER BY card_id 

2 Answers 2

4
SELECT * FROM cards ORDER BY CASE card_id WHEN 7 THEN 1 WHEN 10 THEN 2 WHEN 3 THEN 3 ELSE 4 END, card_id 
Sign up to request clarification or add additional context in comments.

Comments

3

A bit shorter than Quassnoi's query, with FIELD :

-- ... ORDER BY FIELD(card_id, 3, 10, 7) DESC 

You have to invert the order because of the DESC, I didn't find a way to do it more naturally.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.