Mysql - specific item to be first and then to sort the rest of the items
You can use field()
in MySQL.
select id,name from friends order by field(id,5,id)
The 1st parameter in field() means the field you want to sort with, the rest is ordering.
So 5 will be sort first, and the rest from id (without 5). You can do like field(id,5,1,3,id)
if you want 5,1,3 to be in front.
5 can be choose to sort at last by field(id,id,5)
. The 2nd id will exclude 5 from it also.
Example in Laravel:
public function listUsers($query)
{
$anonymos = User::where('name', 'LIKE', '%' . 'Anonymous' . '%')->pluck('id')->toArray();
$id_anonymos = implode(',', $anonymos);
$query->orderByRaw("FIELD(id,$id_anonymos)")
->orderBy('name', 'asc');
}