Mysql 命令为 by,先为 null,后为 DESC

如何按字段排序 DESC,但首先列出 NULL 值?

所以我订了一张桌子:

reuestId | offerId | offerTitle
1        | 1       | Alfa
NULL     | 2       | Beta
2        | 3       | Gamma

我想选择它们,这样结果就是:

NULL | 2 | Beta
2    | 3 | Gamma
1    | 1 | Alfa
51509 次浏览

Try this:

ORDER BY [reuestId] IS NULL DESC, [reuestId] DESC

should work (for mySql)

SELECT *
FROM TableX
ORDER BY (requestId IS NOT NULL)
, requestId DESC

A shorter way: SELECT * FROM table ORDER BY -column

This will sort rows ascending order but the values negated, where top rows are rows with null value.