Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

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);
        ->orderBy('name', 'asc');

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts


Page Facebook