I have been searching for an existing question for this one, but i am not sure what to search for. I am looking for a way to order my results based on a list of id’s and a second variable.
Imagine the following record set
user_id user_name
------------------
1 Peter
2 John
3 Luke
4 Tim
5 George
6 Michael
~ etcetera
Now, I have a list of user ids (lets say 1,4,5) that I want the result of my query to start with.
So i thought:
// Edit: replaced 'AND' with ','
SELECT * FROM `user` ORDER BY `user_id` IN (1,4,5), `name`
Would return something like:
user_id user_name
------------------
1 Peter <-- Notice the 1,4,5 at the top
4 Tim
5 George
2 John <-- After that its just alphabetical
3 Luke
6 Michael
~ etcetera
But it doesn’t.. It looks like it just ignores the sorting directives. Does anyone know what I am doing wrong? Is it even possible to do something like this?
Try this: