Brian
Brian

Reputation: 211

specific ordering in mysql

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

Upvotes: 3

Views: 68

Answers (2)

Vincent Savard
Vincent Savard

Reputation: 35917

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.

Upvotes: 3

Quassnoi
Quassnoi

Reputation: 425281

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

Upvotes: 4

Related Questions