开发者

Force to order a certain item first [duplicate]

This question already has answers here: Closed 11 years ago.
开发者_如何转开发

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜