Force to order a certain item first [duplicate]
开发者_如何转开发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?
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.
Perform two queries. The first query will return only the record with ID=12, and the second query will return all records with an ID other than 12.
SELECT * FROM <table> WHERE id=12;
SELECT * FROM <table> WHERE id!=12;
精彩评论