Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force to order a certain item first [duplicate]

Tags:

mysql

Possible Duplicate:
MySQL Query to pull items, but always show a certain one at the top

Hi I have a number of items in a database table.

At the moments they're sorted by name.

But I have one item with ID 12 which I would like to always be first in the line.

Is this possible in an easy way?

like image 536
Brian Avatar asked Jan 18 '23 17:01

Brian


1 Answers

You can do

ORDER BY (id = 12) DESC, someOtherColumn

This will order by whether id equals 12 first (resulting in either 0 or 1, hence the DESC to put the positive results first), then any other column(s) you may specify for sorting.

like image 132
Pekka Avatar answered Jan 28 '23 19:01

Pekka