如何通过字段订购DESC,但首先列出NULL值?
所以我有一张桌子:
reuestId | offerId | offerTitle
1 | 1 | Alfa
NULL | 2 | Beta
2 | 3 | Gamma
我想选择它们,结果如下:
NULL | 2 | Beta
2 | 3 | Gamma
1 | 1 | Alfa
答案 0 :(得分:141)
试试这个:
ORDER BY [reuestId] IS NULL DESC, [reuestId] DESC
应该有效(对于mySql)
答案 1 :(得分:26)
SELECT *
FROM TableX
ORDER BY (requestId IS NOT NULL)
, requestId DESC